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.

7
  • 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. Commented Dec 2, 2016 at 4:11
  • Possible duplicate of How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET? Commented Dec 2, 2016 at 4:14
  • @bansi could you please point out which answer applies to my question? Especially knowing JSON.net does not support Unity natively. Commented Dec 2, 2016 at 4:16
  • @bansi this is a kind of duplicate but not exact because the answer suggested is JSON.Net which OP doesnt want. Commented Dec 2, 2016 at 4: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. Commented Dec 2, 2016 at 4:23

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

3 Comments

You are right, JsonValue is close enough for my case, do see this SO question for more complete usage of JsonValue: stackoverflow.com/questions/9573119/…
Oh wait, except Unity doesn't have System.Json namespace, as it's .Net 3.0-ish...
Yeah it is introduced in 4.5
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.

Comments

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));
    }

Comments

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.