TeamSirenix / odin-serializer

Fast, robust, powerful and extendible .NET serializer built for Unity
http://www.odininspector.com
Apache License 2.0
1.69k stars 193 forks source link

"SerializationUtility.SerializeValue" Dictionary not valid json type #6

Closed pagongamedev closed 5 years ago

pagongamedev commented 6 years ago

i try to Serialization class to json but "key" of dictionary is not valid json type

example

 "$rcontent": [
            {
                0, <-------------------------------------[invalid json type].
                { ... }<-------------------------------------[invalid json type]
            },
            {
               1, <-------------------------------------[invalid json type]
               { ... }<-------------------------------------[invalid json type]
            }
]

i think it must look like this

 "$rcontent": [
              {
                  "$key": 0, <-------------------------------------[valid json type]
                  "$content" : { ... }<-------------------------------------[valid json type]
              },
              {
                  "$key": 1, <-------------------------------------[valid json type]
                 "$content" : { ... }<-------------------------------------[valid json type]
               }
]

test code

 byte[] bytes = SerializationUtility.SerializeValue(sr, DataFormat.JSON);
        string str = System.Text.Encoding.Default.GetString(bytes);
        Debug.Log(str);

test output


{
    "$id": 0,
    "$type": "0|DatabaseManager+SROceanTest, Assembly-CSharp",
    "serializationData": {
        "$type": "1|Sirenix.Serialization.SerializationData, Sirenix.Serialization",
        "SerializedFormat": 0,
        "SerializedBytes": null,
        "ReferencedUnityObjects": null,
        "SerializedBytesString": null,
        "Prefab": null,
        "PrefabModificationsReferencedUnityObjects": null,
        "PrefabModifications": null,
        "SerializationNodes": null
    },
    "jsOceanFish": {
        "$id": 1,
        "$type": "2|System.Collections.Generic.Dictionary`2[[System.Int32, mscorlib],[JSOceanFish, Assembly-CSharp]], mscorlib",
        "comparer": {
            "$id": 2,
            "$type": "3|System.Collections.Generic.GenericEqualityComparer`1[[System.Int32, mscorlib]], mscorlib"
        },
        "$rlength": 3,
        "$rcontent": [
            {
                0, <-------------------------------------[invalid json type]
                {
                    "$id": 3,
                    "$type": "4|JSOceanFish, Assembly-CSharp",
                    "sNameTH": "name 0",
                    "sNameEN": null,
                    "iIDFish": 0,
                    "sThumbnail": null,
                    "sDescription": null,
                    "sImageList": {
                        "$id": 4,
                        "$type": "5|System.Collections.Generic.List`1[[System.String, mscorlib]], mscorlib",
                        "$rlength": 3,
                        "$rcontent": [
                            "image 0",
                            "image 1",
                            "image 2"
                        ]
                    }
                }
            },
            {
                1,<-------------------------------------[invalid json type]
                {
                    "$id": 5,
                    "$type": 4,
                    "sNameTH": "name 1",
                    "sNameEN": null,
                    "iIDFish": 1,
                    "sThumbnail": null,
                    "sDescription": null,
                    "sImageList": {
                        "$id": 6,
                        "$type": 5,
                        "$rlength": 3,
                        "$rcontent": [
                            "image 0",
                            "image 1",
                            "image 2"
                        ]
                    }
                }
            },
            {
                2, <-------------------------------------[invalid json type]
                {
                    "$id": 7,
                    "$type": 4,
                    "sNameTH": "name 2",
                    "sNameEN": null,
                    "iIDFish": 2,
                    "sThumbnail": null,
                    "sDescription": null,
                    "sImageList": {
                        "$id": 8,
                        "$type": 5,
                        "$rlength": 3,
                        "$rcontent": [
                            "image 0",
                            "image 1",
                            "image 2"
                        ]
                    }
                }
            }
        ]
    }
}   
ashikns commented 5 years ago

+1. Is there any reason it's handled like this or is it a bug?

TorVestergaard commented 5 years ago

The ability to write an entry without a name was originally added to save space in collections, without realizing that when used in cases like this it would, strictly speaking, result in invalid JSON. So far, we've just let it be, since it does save space, and (so far as we know) the only serializer that can do something useful with Odin-generated JSON is Odin itself. It seemed harmless, in short.

It is very easily remedied if a compelling case is made to fix it, though.

ashikns commented 5 years ago

I'm trying to use Odin as a serializer for the microsoft graph sdk. The current issue is that the library does a weirdish thing in it's source. When a successful json response is received it takes reponse headers of the http request (which is a dictionary), serializes it using Odin, and then appends it to the response json manually through string editing. So now the response contains both Odin serialized data and normal response json. Odin breaks with this.

TorVestergaard commented 5 years ago

Sorry for the long delay; I've had a leave from work, and have also been sick on top of that. I just committed a fix that gives names to key ("$k") and value ("$v") entries for all dictionary formatters. Hopefully that should resolve this.