Closed juniormayhe closed 5 years ago
Can you add skip default to the settings of your class? Thanks
It seems SkipDefaultValue = true
is removing nullable fields from serialized string.
I was hoping that NetJSON generate the same output as Newtonsoft. This is a scenario where we already have serialized strings persisted in database (created by newtonsoft) and want to migrate solution to NetJSON.
The idea is to keep the same data format to have backward compatibilty between different serialization frameworks and avoid any issues that might happen with deserialization. If migration is possible we would like to have a go.
Here is the expected behavior
Datatype | Newtonsoft serialized string | NetJSON current serialized string | NetJSON expected serialized string |
---|---|---|---|
int id=0 | "id": 0 | none, suppressed (by SkipDefaultValue=true) | "id": 0 |
int? id=null | "id": null | none, suppressed (by SkipDefaultValue=true) | "id": null |
Since your data is already string. By using skip default = true. it will give you the expected value when deserialized that would match the object graph that newtonsoft creates. The generated string been sent should not matter much because no matter what serializer you use at the end of the day. It will output matching result.
If you Jil, and it does not find "id=null" in your payload, it will not set the nullable. The absence of a field means no setting of the property. Unless you are saying that the constructor of your object already set some default values in which you want to override. Making the skipDefault to false and returning null for primitive type is not difficult to do since all i have to do is check is hasValue
int id = 0;
int? id = null;
Let me know your thought. Thanks
This is the behavior i get if i change it based on what you said.
int? id = null
int? id = 0 give the following with skipDefault = false
Please verify if the changes i did solves your problem by using the code from the branch. If so, i can create a nuget package for it.
Thanks,
I am trying to understand nullable serialization with NetJSON. My code:
myObject is a class with
serialized string outputs
instead of
is this expected @rpgmaker ?