0

I would like to load a string that was saved as a base64, but I always get this error. I am using the SimpleJson class (http://wiki.unity3d.com/index.php/SimpleJSON) :

Exception: Error deserializing JSON. Unknown tag: 66 SimpleJSON.JSONNode.Deserialize (System.IO.BinaryReader aReader) (at Assets/plugins/SimpleJSON.cs:512)

My code :

var I = new JSONClass();
I["author"]["name"] = "testName";
I["author2"]["name2"] = "testName2";
string str = I.SaveToCompressedBase64();
//output : QlpoOTFBWSZTWdFZTaIAAAdNgH/gEAAA etc.

//#Error deserializing JSON
string res = JSONClass.LoadFromBase64( str );//.ToString();

Here are the methods from the class :

public static JSONNode LoadFromBase64(string aBase64)
        {
            var tmp = System.Convert.FromBase64String(aBase64);
            var stream = new System.IO.MemoryStream(tmp);
            stream.Position = 0;
            return LoadFromStream(stream);
        }

public static JSONNode LoadFromStream(System.IO.Stream aData)
        {
            using(var R = new System.IO.BinaryReader(aData))
            {
                return Deserialize(R);
            }
        }


public static JSONNode Deserialize(System.IO.BinaryReader aReader)
        {
            JSONBinaryTag type = (JSONBinaryTag)aReader.ReadByte();
            switch(type)
            {
            case JSONBinaryTag.Array:
            {
                int count = aReader.ReadInt32();
                JSONArray tmp = new JSONArray();
                for(int i = 0; i < count; i++)
                    tmp.Add(Deserialize(aReader));
                return tmp;
            }
            case JSONBinaryTag.Class:
            {
                int count = aReader.ReadInt32();                
                JSONClass tmp = new JSONClass();
                for(int i = 0; i < count; i++)
                {
                    string key = aReader.ReadString();
                    var val = Deserialize(aReader);
                    tmp.Add(key, val);
                }
                return tmp;
            }
            case JSONBinaryTag.Value:
            {
                return new JSONData(aReader.ReadString());
            }
            case JSONBinaryTag.IntValue:
            {
                return new JSONData(aReader.ReadInt32());
            }
            case JSONBinaryTag.DoubleValue:
            {
                return new JSONData(aReader.ReadDouble());
            }
            case JSONBinaryTag.BoolValue:
            {
                return new JSONData(aReader.ReadBoolean());
            }
            case JSONBinaryTag.FloatValue:
            {
                return new JSONData(aReader.ReadSingle());
            }

            default:
            {
                throw new Exception("Error deserializing JSON. Unknown tag: " + type);
            }
            }
        }

Thanks

3
  • You are trying to parse a compressed string. Try uncompressing the base64 string before trying to parse it. Commented Nov 28, 2013 at 3:15
  • @rdodev thanks, could you tell me how to do it? I try var base64 = System.Convert.FromBase64String(str); but it does not seem to be the way to do it. Commented Nov 28, 2013 at 3:32
  • My answer below should help. Commented Nov 28, 2013 at 3:37

1 Answer 1

1

The problem you are having is that you are trying to save to a compressedbase64string here:

string str = I.SaveToCompressedBase64();

which is giving you trouble when you try to parse it and uncompress it. So, I suggest you use their SaveToBase64() as follows"

string str = I.SaveToBase64();

And leave the rest of your program unchanged (unless there's another error there I didn't see).

Another approach is to use their LoadFromCompressedBase64(), so your code would look the same with the exception of:

string res = JSONClass.LoadFromCompressedBase64( str );//.ToString();

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! I was close :) I did not pay attention to the "compressed" in the method, very nice, thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.