Skip to content

Instantly share code, notes, and snippets.

@onionmk2
Last active August 17, 2022 20:08
Show Gist options
  • Save onionmk2/d2e3e4cca27a37a89796e084e05de212 to your computer and use it in GitHub Desktop.
Save onionmk2/d2e3e4cca27a37a89796e084e05de212 to your computer and use it in GitHub Desktop.

Revisions

  1. onionmk2 renamed this gist Jun 23, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion NewBehaviourScript.cs → UsingJsonDotNetInUnity.cs
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    using Newtonsoft.Json;
    using UnityEngine;

    public class NewBehaviourScript : MonoBehaviour
    public class UsingJsonDotNetInUnity : MonoBehaviour
    {
    private void Awake()
    {
  2. onionmk2 revised this gist Jun 23, 2017. 1 changed file with 13 additions and 4 deletions.
    17 changes: 13 additions & 4 deletions NewBehaviourScript.cs
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    using System;
    using System.Collections.Generic;
    using System.IO;
    using Newtonsoft.Json;
    using UnityEngine;

    @@ -28,7 +29,7 @@ private void Awake()

    var accountOnion = new Account
    {
    Email = "onion@example.com",
    Email = "onion@example.co.uk",
    Active = true,
    CreatedDate = new DateTime(2013, 1, 20, 0, 0, 0, DateTimeKind.Utc),
    Roles = new List<string>
    @@ -49,11 +50,19 @@ private void Awake()
    setting.Formatting = Formatting.Indented;
    setting.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

    var accounts = new List<Account> {accountJames, accountOnion};
    // write
    var accountsFromCode = new List<Account> {accountJames, accountOnion};
    var json = JsonConvert.SerializeObject(accountsFromCode, setting);
    var path = Path.Combine(Application.dataPath, "hi.json");
    File.WriteAllText(path, json);

    var json = JsonConvert.SerializeObject(accounts, setting);
    // read
    var fileContent = File.ReadAllText(path);
    var accountsFromFile = JsonConvert.DeserializeObject<List<Account>>(fileContent);
    var reSerializedJson = JsonConvert.SerializeObject(accountsFromFile, setting);

    print(json);
    print(reSerializedJson);
    print("json == reSerializedJson is" + (json == reSerializedJson));
    }

    public class Account
  3. onionmk2 revised this gist Jun 23, 2017. No changes.
  4. onionmk2 revised this gist Jun 23, 2017. No changes.
  5. onionmk2 revised this gist Jun 23, 2017. 1 changed file with 13 additions and 2 deletions.
    15 changes: 13 additions & 2 deletions NewBehaviourScript.cs
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,12 @@ private void Awake()
    "User",
    "Admin"
    },
    Ve = new Vector3(10, 3, 1)
    Ve = new Vector3(10, 3, 1),
    StrVector3Dictionary = new Dictionary<string, Vector3>
    {
    {"start", new Vector3(0, 0, 1)},
    {"end", new Vector3(9, 0, 1)}
    }
    };


    @@ -31,7 +36,12 @@ private void Awake()
    "User",
    "Admin"
    },
    Ve = new Vector3(0, 3, 1)
    Ve = new Vector3(0, 3, 1),
    StrVector3Dictionary = new Dictionary<string, Vector3>
    {
    {"vr", new Vector3(0, 0, 1)},
    {"pc", new Vector3(9, 9, 1)}
    }
    };


    @@ -53,5 +63,6 @@ public class Account
    public DateTime CreatedDate { get; set; }
    public IList<string> Roles { get; set; }
    public Vector3 Ve { get; set; }
    public Dictionary<string, Vector3> StrVector3Dictionary { get; set; }
    }
    }
  6. onionmk2 revised this gist Jun 23, 2017. 1 changed file with 32 additions and 14 deletions.
    46 changes: 32 additions & 14 deletions NewBehaviourScript.cs
    Original file line number Diff line number Diff line change
    @@ -5,26 +5,44 @@

    public class NewBehaviourScript : MonoBehaviour
    {
    private readonly Account account = new Account
    private void Awake()
    {
    Email = "[email protected]",
    Active = true,
    CreatedDate = new DateTime(2013, 1, 20, 0, 0, 0, DateTimeKind.Utc),
    Roles = new List<string>
    var accountJames = new Account
    {
    "User",
    "Admin"
    },
    Ve = new Vector3(10, 3, 1)
    };
    Email = "[email protected]",
    Active = true,
    CreatedDate = new DateTime(2013, 1, 20, 0, 0, 0, DateTimeKind.Utc),
    Roles = new List<string>
    {
    "User",
    "Admin"
    },
    Ve = new Vector3(10, 3, 1)
    };


    var accountOnion = new Account
    {
    Email = "[email protected]",
    Active = true,
    CreatedDate = new DateTime(2013, 1, 20, 0, 0, 0, DateTimeKind.Utc),
    Roles = new List<string>
    {
    "User",
    "Admin"
    },
    Ve = new Vector3(0, 3, 1)
    };


    private void Awake()
    {
    var setting = new JsonSerializerSettings();
    setting.Formatting = Formatting.Indented;
    setting.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; // if I comment out this line, I can not create json.
    setting.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

    var accounts = new List<Account> {accountJames, accountOnion};

    var json = JsonConvert.SerializeObject(accounts, setting);

    var json = JsonConvert.SerializeObject(account, setting);
    print(json);
    }

  7. onionmk2 revised this gist Jun 23, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion NewBehaviourScript.cs
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@ private void Awake()
    {
    var setting = new JsonSerializerSettings();
    setting.Formatting = Formatting.Indented;
    setting.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
    setting.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; // if I comment out this line, I can not create json.

    var json = JsonConvert.SerializeObject(account, setting);
    print(json);
  8. onionmk2 created this gist Jun 23, 2017.
    39 changes: 39 additions & 0 deletions NewBehaviourScript.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    using System;
    using System.Collections.Generic;
    using Newtonsoft.Json;
    using UnityEngine;

    public class NewBehaviourScript : MonoBehaviour
    {
    private readonly Account account = new Account
    {
    Email = "[email protected]",
    Active = true,
    CreatedDate = new DateTime(2013, 1, 20, 0, 0, 0, DateTimeKind.Utc),
    Roles = new List<string>
    {
    "User",
    "Admin"
    },
    Ve = new Vector3(10, 3, 1)
    };

    private void Awake()
    {
    var setting = new JsonSerializerSettings();
    setting.Formatting = Formatting.Indented;
    setting.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

    var json = JsonConvert.SerializeObject(account, setting);
    print(json);
    }

    public class Account
    {
    public string Email { get; set; }
    public bool Active { get; set; }
    public DateTime CreatedDate { get; set; }
    public IList<string> Roles { get; set; }
    public Vector3 Ve { get; set; }
    }
    }