Closed thecodrr closed 1 year ago
Hi,
Can you enable NetJson.IncludeTypeInformation = true
/// <summary>
/// Enable including type information for serialization and deserialization
/// </summary>
public bool IncludeTypeInformation { get; set; }
In regards to CanSerialize and SerializeAs. SerializeAs is used for object type serialization. And can canSerialize only allows you to control was is visible for serialization
The type Information flag will let it include the type details to know how to serialize the data itself.
@rpgmaker Okay, IncludeTypeInformation = true
made it work as expected however there is this unnecessary $type
key. I am sending this data over the network to be consumed by a JS Web App. I could simply ignore it of course but is there a way to filter it out?
There currently not a way to filter it out as when you get the data back, it will not know what type to use to deserialize. The only viable solution is to make a custom solution on your side that will use a number and based on that number you will use it to determine what type to call deserialization with as you don't need to use generic deserialization methods.
All this will need to be in your custom wrapper class.
@rpgmaker How about a TypeMap kind of? I can specify to it which interface is implemented by which class and it can use that information.
It might add some lookup time but as it will be optional, why not?
There is one here. I did not document it and have not really tried to use it much. NetJSON.RegisterTypeSerializer . It will need you to do your custom serialization and return a full valid json. It will get compiled into the serialization itself. So the cost of look up only happens once per type and will perform based on the static method you register.
/// <summary>
/// Register serializer primitive method for <typeparamref name="T"/> when object type is encountered
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="serializeFunc"></param>
public static void RegisterTypeSerializer<T>(Func<T, string> serializeFunc)
There is also one for deserialization too.
@rpgmaker Okay so I tried making it work like:
NetJSON.NetJSON.RegisterCustomTypeSerializer<ISubscription>(NetJSONHelper.CustomTypeSerializer);
//NetJSONHelper.cs
public static void CustomTypeSerializer<T>(T obj, StringBuilder builder, NetJSON.NetJSONSettings settings)
{
if (obj is ISubscription)
{
builder.Append(NetJSON.NetJSON.Serialize<Subscription>(obj as Subscription));
}
}
But builder.Append
replaces the whole string. How do I make it actually append?
What do you mean by replace whole string. Do you mean it does not preserve the property that hold subscription?
I will have to test it again as this was originally intended for object serialization until I adapted it. I will expect thr behavior for it to simple just add to existing output.
@rpgmaker It doesn't add to the existing output.
Before builder.Append
, builder holds this:
{"appData":{"subscription":}
After:
{"expiry":"\/Date(15992888338610000)\/","isTrial":true}{"expiry":"\/Date(15992888338610000)\/","isTrial":true}
And the overall result becomes:
"{\"expiry\":\"\\/Date(15992888338610000)\\/\",\"isTrial\":true}{\"expiry\":\"\\/Date(15992888338610000)\\/\",\"isTrial\":true}}}"
I have no idea why its adding it twice.
Interesting. One thing to point out is that current implementation is mostly based on dealing with object as mentioned previously. So it will not deal with writing out the property/field name too. As for the duplicate and overwrite, that looks like a bug. I will try to fix that this weekend if time permits.
But the implementation will still most likely not handle the naming portion because it will take time to embed it to replace the il emitted code since it will require generating code that will need to do some kind of reflection to use the method as it is.
So it will not deal with writing out the property/field name too I am not sure I quite understood what you mean.
The CustomTypeSerializer
function is simply using NetJSON
's own Serialize
function so why is property/field name going to be an issue?
As for the duplicate and overwrite, that looks like a bug. I will try to fix that this weekend if time permits. Cool.
@rpgmaker as a side question, is there a way to give NetJSON a default settings? I see there is NetJSONSettings.CurrentSettings
but it is not respected when I call NetJSON.Serialize
.
Also, why do we not have async implementations? Is there a reason for this? Is wrapping the Serialize/Deserialize in Task.Run
enough?
In regards to serialize. If you look at the current code, the starting point checks if method is registered and return by calling the custom method. But if it is not then it uses the default serialize logic which is responsible for not just serialization but also adding the name of the field or property.
Since I am emitting code and saving it to dynamic assembly in memory, I will need to bypass the portion of the code to make it to honor the custom method at the right point in the call stack.
If i remember, the default setting is kinda of static and is not meant to be used as it is which is why i have the option to pass yours. All the methods that controls the default settings are deprecated, and call them will update the default settings.
Wrapping in task.run should be enough as long as you are using your own settings object. The code is very thread safe for most operations.
Is this still an issue?
I am working on a solution to make this more useful for custom serializer types, so NETJson can be extended easily.
So I just verified that the custom serialization actually does work as expected. The problem with the replacement occurs when you try to call NetJSON.Serialize inside the custom serialize which might be causing an issue with the generator.
As shown below, it yield the expected result in both scenario
With custom serializer only and manual building of json.
With custom serializer/deserializer and manual building of json
@thecodrr , Let me know if the above makes sense to you.
Closing issue. Reopen if still needed @thecodrr
Okay, I just tried NetJSON and it is not serializing Interface properties properly. Here is my data structure:
Now when I try to serialize the
User
object using:It gives me this JSON:
I have overriden
CanSerialize
andSerializeAs
to respectDataMember
andIgnoreDataMember
attributes appropriately.In the
Subscription
object,IsTrial = true
andIsRecurring = false
which is never serialized. I don't know what is the issue here. Also,Expiry
key should beexpiry
.Hope you can help.