Unity/Database

[์œ ๋‹ˆํ‹ฐ Json ํŒŒ์‹ฑ] Newtonsoft.Json & Unity JSON Utility

๐ŸŸฆ Newtonsoft.Json

๐ŸŸง Document

์ฐธ๊ณ ์˜์ƒ : ๋ฒ ๋ฅด์˜ ๊ฒŒ์ž„ ๊ฐœ๋ฐœ ์œ ํŠœ๋ธŒ

https://docs.microsoft.com/ko-kr/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0 

 

C#์„ ์‚ฌ์šฉํ•˜์—ฌ JSON์„ ์ง๋ ฌํ™” ๋ฐ ์—ญ์ง๋ ฌํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ• - .NET

System.Text.Json ๋„ค์ž„์ŠคํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ .NET์—์„œ JSON์œผ๋กœ ์ง๋ ฌํ™” ๋ฐ ์—ญ์ง๋ ฌํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ด…๋‹ˆ๋‹ค. ์ƒ˜ํ”Œ ์ฝ”๋“œ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

docs.microsoft.com

 

 

 

๐ŸŸง Settings

https://www.newtonsoft.com/json

- ๋งํฌ ์ ‘์† ํ›„ Download ๋ฒ„ํŠผ ํด๋ฆญ

 

 

 

- Json.NET ๋ฒ„ํŠผ ํด๋ฆญ

 

 

 

- ์ตœ์‹  ๋ฒ„์ „์˜ zipํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ ํ›„ ์••์ถ• ํ•ด์ œ

 

 

 

- Bin\net45์— ์žˆ๋Š” dll ํŒŒ์ผ์„ ์œ ๋‹ˆํ‹ฐ๋กœ ๋“œ๋ž˜๊ทธ ์•ค ๋“œ๋กญ

 

 

 

- Project Settings / Player / Other Settings / Api Comatibility Level : .NET 4.X

 

 

 

๐ŸŸง Object to Json

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;

public class JsonTest : MonoBehaviour
{
    public JsonTestClass jsonTest_1;
    void Start()
    {
        string jsonData = JsonConvert.SerializeObject(jsonTest_1);
        Debug.Log(jsonData);
    }

    [System.Serializable]
    public class JsonTestClass
    {
        public int i;
        public float f;
        public bool b;
        public string str;
        public int[] array;
        public List<int> list = new List<int>();
        public Dictionary<string, float> dictionary = new Dictionary<string, float>();
        public Vector2Int vector;

        // ์ƒ์„ฑ์ž
        public JsonTestClass()
        {
            i = 1;
            f = 2.2f;
            b = true;
            str = "JSON Test String";
            array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1 };

            for (int i = 0; i < 5; i++)
            {
                list.Add(i * 2);
            }

            dictionary.Add("PI", Mathf.PI);
            dictionary.Add("Epsilon", Mathf.Epsilon);
            dictionary.Add("Sqrt(2)", Mathf.Sqrt(2));

            vector = new Vector2Int(4, 5);
        }

        public void Print()
        {
            Debug.Log($"i = {i}");
            Debug.Log($"f = {f}");
            Debug.Log($"b = {b}");
            Debug.Log($"str = {str}");

            for (int i = 0; i < array.Length; i++)
            {
                Debug.Log($"array[{i}] = {array[i]}");
            }
            for (int i = 0; i < list.Count; i++)
            {
                Debug.Log($"list[{i}] = {list[i]}");
            }
            foreach (var item in dictionary)
            {
                Debug.Log($"dictionary : key = {item.Key} / value = {item.Value}");
            }
            Debug.Log($"vector = {vector}");
        }

    }
}

- ์Šคํฌ๋ฆฝํŠธ ์ƒ์„ฑ ํ›„ ์ฝ”๋“œ ์ž…๋ ฅ

 

 

 

- ๋นˆ ๊ฒŒ์ž„ ์˜ค๋ธŒ์ ํŠธ์— ์Šคํฌ๋ฆฝํŠธ ์ถ”๊ฐ€

 

 

 

- ํ”Œ๋ ˆ์ดํ•˜๋ฉด ๋ฌธ์ž์—ด๋กœ ๋ฐ”๋€ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Œ

 

 

 

๐ŸŸง Object to Json

    public JsonTestClass jsonTest_1;
    void Start()
    {
        string jsonData = JsonConvert.SerializeObject(jsonTest_1);
        Debug.Log(jsonData);
        
        JsonTestClass jtest2 = JsonConvert.DeserializeObject<JsonTestClass>(jsonData);
        jtest2.Print();
    }

- ๋ฌธ์ž์—ด๋กœ ๋ฐ”๋€ ๋ฐ์ดํ„ฐ๋ฅผ jsonํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ํ–ˆ๋‹ค. 

 

 

 

๐ŸŸฆ Unity JSON Utility

UnityEngine ๋„ค์ž„์ŠคํŽ˜์ด์Šค์— ํฌํ•จ๋˜์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์— Newtonsoft์™€ ๋‹ฌ๋ฆฌ ์„ค์น˜ํ•˜๋Š” ๊ณผ์ •์ด ์—†๋‹ค.

 

 

 

๐ŸŸง Document

https://docs.unity3d.com/ScriptReference/JsonUtility.html

 

 

 

๐ŸŸง ์‚ฌ์šฉํ•ด๋ณด๊ธฐ

void Start()
{
    string jsonData = JsonUtility.ToJson(jsonTest_1);
    Debug.Log(jsonData);

    JsonTestClass jtest2 = JsonUtility.FromJson<JsonTestClass>(jsonData);
    jtest2.Print();
}

 

 

 

๐ŸŸฆ ์ •๋ฆฌ ๋ฐ ๋น„๊ต

๐ŸŸง Newtonsoft.Json

- Newtonsoft.Json ๋„ค์ž„์ŠคํŽ˜์ด์Šค ์ถ”๊ฐ€

- Object to Json : JsonConvert.SerializeObject(๋ฐ์ดํ„ฐ);

- Json to Object : JsonConvert.DeserializeObject<ํด๋ž˜์Šคํ˜•์‹>(๋ฐ์ดํ„ฐ);

 

 

 

๐ŸŸง Unity JSON Utility

- ๋„ค์ž„์ŠคํŽ˜์ด์Šค ์ถ”๊ฐ€ํ•  ํ•„์š” ์—†์Œ (UnityEngine)

- Object to Json : JsonUtility.ToJson(๋ฐ์ดํ„ฐ);

- Json to Object : JsonUtility.FromJson<ํด๋ž˜์Šคํ˜•์‹>(๋ฐ์ดํ„ฐ);

 

 

 

๐ŸŸง ์ด์ค‘๋ฐฐ์—ด

- Unity JSON Utility๋Š” ์ด์ค‘๋ฐฐ์—ด ๋ณ€ํ™˜์„ ์ง€์›ํ•˜์ง€ ์•Š์•˜๊ณ , Newtonsoft.Jsons๋Š” ์ง€์›ํ–ˆ๋‹ค.

 

 

 

{
    "scores": [
        [
            0,
            1
        ],
        [
            2,
            3,
            4
        ]
    ]
}

- TempJson.json ํŒŒ์ผ์„ ์ž‘์„ฑํ–ˆ๋‹ค.

 

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class JsonTest : MonoBehaviour
{
    void Start()
    {
        string temp_json = Resources.Load<TextAsset>("TempJson").text;
        TempClass temp_Jutilty = JsonUtility.FromJson<TempClass>(temp_json);
        temp_Jutilty.Print();
    }


    [System.Serializable]
    public class TempClass
    {
        public int[][] scores;

        public void Print()
        {
            foreach (var score in scores)
            {
                Debug.Log("----- score -----");
                foreach (var i in score)
                {
                    Debug.Log($"{i}");
                }
            }
        }
    }

}

- JsonUtility๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ณ€ํ™˜ํ•ด๋ณด๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

- ๋ณ€ํ™˜์ด ์•ˆ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— scores ๋ณ€์ˆ˜์—์„œ Null์ด ๋œจ๋Š” ๊ฒƒ์œผ๋กœ ์ถ”์ •๋œ๋‹ค.

 

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;

public class JsonTest : MonoBehaviour
{
    void Start()
    {
        string temp_json = Resources.Load<TextAsset>("TempJson").text;
        TempClass temp_Newtonsoft = JsonConvert.DeserializeObject<TempClass>(temp_json);
        temp_Newtonsoft.Print();
    }

    [System.Serializable]
    public class TempClass
    {
        public int[][] scores;

        public void Print()
        {
            foreach (var score in scores)
            {
                Debug.Log("----- score -----");
                foreach (var i in score)
                {
                    Debug.Log($"{i}");
                }
            }
        }
    }

}

- ๋˜‘๊ฐ™์ด Newtonsoft.Json์„ ์‚ฌ์šฉํ•ด๋ณด๋ฉด ๊ฐ’์ด ์ž˜ ๋‚˜์˜จ๋‹ค.

'Unity > Database' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

04. Azure CosmosDB - Delete  (0) 2021.09.06
03. Azure CosmosDB - Read  (0) 2021.08.29
02. Azure CosmosDB - Insert  (0) 2021.08.28
01. Azure CosmosDB Settings  (0) 2021.08.21
00. Azure ์ฒดํ—˜๊ณ„์ • ํ™œ์„ฑํ™”  (0) 2021.08.20