Exemplo n.º 1
0
    private void JsonUtilitySample()
    {
        // JsonUtility : Unity純正、速いが制約多め
        var target = new PersonSerializableClassField
        {
            id        = 100,
            addresses = new AddressSerializableClassField[]
            {
                new AddressSerializableClassField {
                    zipcode = 1002321, address = "hoge"
                },
                new AddressSerializableClassField {
                    zipcode = 1008492, address = "fuga"
                },
            },
        };

        var serialized = JsonUtility.ToJson(target);

        Debug.Log(serialized);

        var deserialized = JsonUtility.FromJson <PersonSerializableClassField>(serialized);

        Debug.Log(deserialized.ToString());
    }
Exemplo n.º 2
0
    private void NewtonsofJsonSample()
    {
        // Json.NET : C#におけるメジャーどころ、遅いが汎用的で拡張性がある
        {
            var target = new PersonSerializableClassField
            {
                id        = 100,
                addresses = new AddressSerializableClassField[]
                {
                    new AddressSerializableClassField {
                        zipcode = 1002321, address = "hoge"
                    },
                    new AddressSerializableClassField {
                        zipcode = 1008492, address = "fuga"
                    },
                },
            };

            var serialized = JsonConvert.SerializeObject(target);
            Debug.Log(serialized);

            var deserialized = JsonConvert.DeserializeObject <PersonSerializableClassField>(serialized);
            Debug.Log(deserialized.ToString());
        }

        {
            var target = new PersonPlaneClassProperty
            {
                Id        = 200,
                Addresses = new AddressPlaneClassProperty[]
                {
                    new AddressPlaneClassProperty {
                        Zipcode = 2003921, Address = "hoge"
                    },
                    new AddressPlaneClassProperty {
                        Zipcode = 2002955, Address = "fuga"
                    },
                },
            };

            var serialized = JsonConvert.SerializeObject(target);
            Debug.Log(serialized);

            var deserialized = JsonConvert.DeserializeObject <PersonPlaneClassProperty>(serialized);
            Debug.Log(deserialized.ToString());
        }

        {
            var target = new AddressRenamedProperty
            {
                Zipcode         = 3003924,
                Address         = "foobar",
                TelephoneNumber = "0312345678",
            };

            var serialized = JsonConvert.SerializeObject(target);
            Debug.Log(serialized);

            var deserialized = JsonConvert.DeserializeObject <AddressRenamedProperty>(serialized);
            Debug.Log(deserialized.ToString());
        }
    }
Exemplo n.º 3
0
    private void Utf8JsonSample()
    {
        // UTF8Json : neuecc氏作、JsonUtilityと同様速い上に汎用的
        // Dose'nt work on HoloLens2 (IL2CPPxARM64)
        {
            var target = new PersonSerializableClassField
            {
                id        = 100,
                addresses = new AddressSerializableClassField[]
                {
                    new AddressSerializableClassField {
                        zipcode = 1002321, address = "hoge"
                    },
                    new AddressSerializableClassField {
                        zipcode = 1008492, address = "fuga"
                    },
                },
            };
            {
                var serialized = JsonSerializer.ToJsonString(target);
                Debug.Log(serialized);

                var deserialized = JsonSerializer.Deserialize <PersonSerializableClassField>(serialized);
                Debug.Log(deserialized.ToString());
            }

            {
                var serialized = JsonSerializer.Serialize(target);
                Debug.Log(Convert.ToBase64String(serialized));
                var tmp = System.Text.Encoding.UTF8.GetString(serialized);
                Debug.Log(tmp);


                var deserialized = JsonSerializer.Deserialize <PersonSerializableClassField>(serialized);
                Debug.Log(deserialized.ToString());
            }
        }

        {
            var target = new PersonPlaneClassProperty
            {
                Id        = 200,
                Addresses = new AddressPlaneClassProperty[]
                {
                    new AddressPlaneClassProperty {
                        Zipcode = 2003921, Address = "hoge"
                    },
                    new AddressPlaneClassProperty {
                        Zipcode = 2002955, Address = "fuga"
                    },
                },
            };

            var serialized = JsonSerializer.Serialize(target);
            Debug.Log(Convert.ToBase64String(serialized));
            var tmp = System.Text.Encoding.UTF8.GetString(serialized);
            Debug.Log(tmp);

            var deserialized = JsonSerializer.Deserialize <PersonPlaneClassProperty>(serialized);
            Debug.Log(deserialized.ToString());
        }

        {
            var target = new AddressRenamedProperty
            {
                Zipcode         = 3003924,
                Address         = "foobar",
                TelephoneNumber = "0312345678",
            };

            var serialized = JsonSerializer.Serialize(target);
            Debug.Log(Convert.ToBase64String(serialized));
            var tmp = System.Text.Encoding.UTF8.GetString(serialized);
            Debug.Log(tmp);

            var deserialized = JsonSerializer.Deserialize <AddressRenamedProperty>(serialized);
            Debug.Log(deserialized.ToString());
        }
    }