3

I want to avoid importing a huge library to gain full JSON support.

My use case is REALLY simple: I need a parser to handle one specific case of JSON, where both key and value are strings, ie. { "name": "David" }. No nesting, no arrays, no object serialization.

The reason being, I use JSON only for i18n, and I have structure my translation files to be flat JSON.

  • Is it a good idea to hand roll my own parser?
  • Is there one out there already?
  • Are there easier solutions to my problem?

EDIT: yes, I do know JSON.net is the defacto solution for .NET, but it's not the solution for Unity (not natively). I really only need a tiny portion of its power.

bitinn
  • 9,188
  • 10
  • 38
  • 64
  • Note: but I do want the flexibility of not needing to writing a class for my JSON object, which is why I pick Dictionary as my desired output. – bitinn Dec 02 '16 at 04:11
  • Possible duplicate of [How can I deserialize JSON to a simple Dictionary in ASP.NET?](http://stackoverflow.com/questions/1207731/how-can-i-deserialize-json-to-a-simple-dictionarystring-string-in-asp-net) – bansi Dec 02 '16 at 04:14
  • @bansi could you please point out which answer applies to my question? Especially knowing JSON.net does not support Unity natively. – bitinn Dec 02 '16 at 04:16
  • @bansi this is a kind of duplicate but not exact because the answer suggested is JSON.Net which OP doesnt want. – Mohit S Dec 02 '16 at 04:16
  • If you want `only a tiny portion of its power` it is better you hand roll a parser. All generic solutions out there try to go for a complete solution. – bansi Dec 02 '16 at 04:23
  • I have one exception rule: if it's built-in, and does what I want, then I say why not :) JSON.NET isn't though. – bitinn Dec 02 '16 at 04:24
  • OK I found one SO question that address this question: http://stackoverflow.com/questions/9573119/how-to-parse-json-without-json-net-library – bitinn Dec 02 '16 at 04:28

3 Answers3

2

System.Json might do the trick for you.

The JsonValue.Parse() Method parses JSON text and returns a JsonValue like

JsonValue value = JsonValue.Parse(@"{ ""name"": ""David"" }");

You can also have a look at The JavaScriptSerializer class is used internally by the asynchronous communication layer to serialize and deserialize the data that is passed between the browser and the Web server.

var Names = new JavaScriptSerializer().Deserialize<YourNameClass>(json);
Mohit S
  • 13,723
  • 6
  • 34
  • 69
  • You are right, JsonValue is close enough for my case, do see this SO question for more complete usage of JsonValue: http://stackoverflow.com/questions/9573119/how-to-parse-json-without-json-net-library – bitinn Dec 02 '16 at 04:28
  • Oh wait, except Unity doesn't have System.Json namespace, as it's .Net 3.0-ish... – bitinn Dec 02 '16 at 04:42
  • Yeah it is introduced in 4.5 – Mohit S Dec 02 '16 at 04:43
1

OK I found one! https://github.com/zanders3/json

Has decent tests, minimal features and likely designed for my specific use case.

To load a JSON file:

Dictionary<string, object> locales = new Dictionary<string, object>();

TextAsset file = Resources.Load(name) as TextAsset;
var locale = file.text.FromJson<object>();
locales.Add(name, locale);

To use the JSON dictionary:

string activeLocale = "en-US";
var locale = locales[activeLocale] as Dictionary<string, object>;
var translation = locale[key] as string;

Dead simple.

bitinn
  • 9,188
  • 10
  • 38
  • 64
0

Super simple Json parsing for Json strings like: { "name1": "David", "name2": "David" }. If you don't need to handle quoted commas and colons, eg {"weapons":"gun,mashine gun,supergun:2"} - change Regex.Split to simple String.Split.

private Dictionary<string, string> ParseJson(string content)
    {
        content = content.Trim(new[] { '{', '}' }).Replace('\'', '\"');
    
        var trimmedChars = new[] { ' ','\"' };
    
        //regex to split only unquoted separators
        Regex regxComma = new Regex(",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))");
        Regex regxColon = new Regex(":(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))");
        
        return regxComma.Split(content)
                        .Select(v => regxColon.Split(v))
                        .ToDictionary(v => v.First().Trim(trimmedChars), v => v.Last().Trim(trimmedChars));
    }