Unity3d SimpleJSON adds int to JSONClass

I am using SimpleJSON for Unity3d. And I want to add intin JSONClass.

For example, I need json: {"attr" : 4}

JSONClass cl = new JSONClass();

I tried this:

cl["attr"].AsInt = 4;

And this:

cl["attr"] = new JSONData(4);

And any other cases. Anyway, I get {"attr" : "4"}where 4is the string.

How can I add intto it?

+4
source share
2 answers

In the current implementation, this is not possible.

So, I added new types:

public enum JSONBinaryTag
    {
        Array            = 1,
        Class            = 2,
        Value            = 3,
        IntValue        = 4,
        DoubleValue        = 5,
        BoolValue        = 6,
        FloatValue        = 7,
        LongValue         = 8,
        String          = 9,   // <-- new
        Number = 10           // <-- new
    }

And add type checking in JSONData:

public class JSONData : JSONNode{
    static Regex m_Regex = new Regex(@"^[0-9]*(?:\.[0-9]*)?$");
    private JSONBinaryTag m_Type = JSONBinaryTag.String;

    private string m_Data;
    public override string Value {
        get { return m_Data; }
        set { m_Data = value; } 
    }
    public JSONData(string aData){
        m_Data = aData;

        // check for number
        if (m_Regex.IsMatch(m_Data))
            m_Type = JSONBinaryTag.Number;
        else
            m_Type = JSONBinaryTag.String;

    }
    [...]
}

And changed the method toString():

   public override string ToString(){
        if (m_Type == JSONBinaryTag.String)
            return "\"" + Escape(m_Data) + "\"";
        else
            return Escape(m_Data);

    }

Now int, float, doubleit will be added without the number ". And it will look something like this:{"attr" : 4}

+8
source

? https://gist.github.com/lcnvdl/4532671a8f4dbbb6f306

:

JsonHelper.Json(myObject);

, GameObjects, ScriptableObjects, MonoBehaviour ..

+1

Source: https://habr.com/ru/post/1568970/


All Articles