Link with 4 notes
Ages ago I found a script for encoding and decoding JSON and popped it up on the Unity forums. Wasn’t great, but it did the job. Yesterday I took a look at it and spotted a bunch of ways it could be cleaned up and optimized.
Even though I still recommend using JsonFX, MiniJSON can still be useful for situations where you just want something really simple (Array, Dictionary, etc.) encoded and decoded and size is a consideration.
Careful though, this script has changed a lot, so it isn’t a drop-in replacement for the old version.
Link with 3 notes
A couple of people have asked how to use the JsonFx library that I put up on bitbucket. The version I’m using is 1.4 from JsonFx.net, and it is poorly documented.
The two key methods are:
JsonReader.Deserialize<T>(string json);
and
JsonWriter.Serialize(object src);
With Deserialize you need to specify the type (T) you want to unpack to. This can be almost any C# class that has public variables or properties. In the example unitypackage linked above, I created two quick classes for Twitter search results, and an individual tweet.
What’s great about JsonFx is that it can deserialize nested classes, so the array of Tweets that is returned in search results gets deserialized too. This is something that I had issues with in most other libraries I evaluated.
The Serialize method just take the object you want to turn into a JSON string and does its thing.
You then have a string you can post to a web server, using the WWWForm and WWW classes in Unity.
If you don’t want to create your own classes for Serialization/Deserialization, you can just use Dictionary<string,object>, or an Array, or whatever.
There are some other things that are hidden inside JsonFx, like a JsonFx.IgnoreAttribute for public fields that you don’t want to be serialized, etc. You’ll have to root around in the assembly or source code for the exact syntax for that stuff. Remember, I didn’t write JsonFx, I just commented out a couple lines to make it work better in Unity.
Here’s the C# script that’s in the above Unity package:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using DB = UnityEngine.Debug;
using DC = DebugConsole;
using JsonFx.Json;
public class Tweet {
public System.DateTime created_at;
public string from_user;
public long from_user_id;
public string from_user_id_str;
public string from_user_name;
public string geo;
public long id;
public string id_str;
public string iso_language_code;
public Dictionary<string, string> metadata;
public string profile_image_url;
public string source;
public string text;
public long to_user_id;
public string to_user_id_str;
}
public class TwitterSearchResults {
public float completed_in;
public long max_id;
public string max_id_str;
public string next_page;
public int page;
public string query;
public string refresh_url;
public Tweet[] results;
public int results_per_page;
public long since_id;
public string since_id_str;
}
public class JsonFxDemo : MonoBehaviour {
public string query = "#Unity3d";
// Use this for initialization
void Start () {
DC.IsOpen = true;
StartCoroutine(PerformSearch(query));
}
void PrintResults(string rawJson) {
// Raw output:
DB.Log(DC.Log("******** raw string from Twitter ********"));
DB.Log(DC.Log(rawJson));
// Turn the JSON into C# objects
var search = JsonReader.Deserialize<TwitterSearchResults>(rawJson);
// iterate through the array of results;
DB.Log(DC.Log("******** search results ********"));
foreach (var tweet in search.results) {
DB.Log(DC.Log(tweet.from_user_name + " : " + tweet.text));
}
DB.Log(DC.Log("******** serialize an entity ********"));
// this turns a C# object into a JSON string.
string json = JsonWriter.Serialize(search.results[0]);
DB.Log(DC.Log(json));
}
IEnumerator PerformSearch(string query) {
query = WWW.EscapeURL(query);
using (var www = new WWW(string.Format("http://search.twitter.com/search.json?q={0}", query))) {
while (!www.isDone) {
yield return null;
}
PrintResults(www.text);
}
}
}