Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity3d SimpleJSON add int to JSONClass

I'm using SimpleJSON for Unity3d. And i want to add int to 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 4 is string.

How can i add int to it?

like image 476
Suvitruf - Andrei Apanasik Avatar asked Jan 09 '23 05:01

Suvitruf - Andrei Apanasik


1 Answers

In current realization it's 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 toString() method:

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

    }

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

like image 142
Suvitruf - Andrei Apanasik Avatar answered Jan 11 '23 23:01

Suvitruf - Andrei Apanasik