Closed ZOXEXIVO closed 5 years ago
Can you be more specific for the simple object scenario?
You can pull down the code. There is 90+ test for it at the moment. Thanks
My project simply serialize List of SomeModel with
private static readonly NetJSONSettings Options = new NetJSONSettings
{
CamelCase = true
};
public static string ToJson(this object obj)
{
return NetJSON.NetJSON.Serialize(obj, Options);
}
public class SomeModel
{
public MentionModel()
{
Related = new RelatedSomeModel();
}
public long Id { get; set; }
public string Title { get; set; }
public string Text { get; set; }
public DateTime Date { get; set; }
public OtherModel3 Mode3 { get; set; }
public OtherModel Model1 { get; set; }
public OtherModel2 Model2 { get; set; }
public double[] Location { get; set; }
public int SomeCount{ get; set; }
public string SourceUrl { get; set; }
public bool IsApproved { get; set; }
public RelatedSomeModelRelated { get; set; }
public DateTime? SomeDate{ get; set; }
public class RelatedSomeModel
{
public RelatedSomeModel()
{
Players = new List<int>();
Clubs = new List<int>();
}
public List<int> SomeCollection { get; set; }
public List<int> SomeCollection { get; set; }
}
}
Result:
Thanks for the example. Could you please provide a fully working example if possible?
Thanks,
A working example with the failure case will be nice for me to debug the issue and resolve. Thanks
It caused only if model passed as object. Object type must be provider for generic ToJson extension, that worked for all serializers
public class MentionModel
{
public long Id { get; set; }
public string Title { get; set; }
public string Text { get; set; }
}
class Program
{
private static readonly NetJSONSettings Options = new NetJSONSettings
{
CamelCase = true
};
static async Task Main(string[] args)
{
object model = new List<MentionModel>
{
new MentionModel
{
Text = "test",
Id = 23232,
Title = "test",
},
new MentionModel
{
Text = "test",
Id = 23232,
Title = "test",
}
};
var resultStr = NetJSON.NetJSON.Serialize(model, Options);
//resultStr = "[,]"
}
}
That is normal. It is not expected to work. It is by design
Object serialization only support primitive type as the wiki describe. It is not Netjson fully broken, if you specific the type via typeof as an additional parameter it will work fine. Please change the title as it does not match what you are describing.😀
Thanks,
Would it not be best to manually call .GetType within your code and use the overload that accept system.Type?
Thanks,
netcoreapp2.2 console application
You need to use SerializeObject method since that will do the get type of the model for you underlying it. I can push the changes to nuget for you.
I have pushed the changes to nuget. So you can now use the SerializeObject method now. It is in 1.2.10.1 version
@rpgmaker it works! thank you
I tried to replace Jil serilizer by NetJSON, but it not worked for simple cases! It my second attemp to use it (first was 2-3 years ago).
How you test your serializer?
I have some JsonExtensions that use some serialization settings. I can use many other serializers and it all works well, except NetJSON. It not worked!
How can develop serializer, that fail serialization of simple objects?
//P.S It serialize array of simple objects as [,,,,,,,]