JsonUtility 1.1.0.1

.NET Framework 4.5.2
NuGet\Install-Package JsonUtility -Version 1.1.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
dotnet add package JsonUtility --version 1.1.0.1
<PackageReference Include="JsonUtility" Version="1.1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add JsonUtility --version 1.1.0.1
#r "nuget: JsonUtility, 1.1.0.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install JsonUtility as a Cake Addin
#addin nuget:?package=JsonUtility&version=1.1.0.1

// Install JsonUtility as a Cake Tool
#tool nuget:?package=JsonUtility&version=1.1.0.1
public partial class JsonUtilityTester : Form
{
    public JsonUtilityTester()
    {
        Button button = new Button() { Text = "Open", Dock = DockStyle.Top, Parent = this, };
        button.Click += (sender, e) =>
        {
            OpenFileDialog ofd = new OpenFileDialog() { InitialDirectory = Application.StartupPath, };
            /** select a file created by clickling by Create Button or create .\text.txt before and paste
             {"name":"foo", "age":"50", "children":{"name":"bar", "age":"20", "gender":"female","hobby":["tennis","swimming","walking"]}}
            */
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                using (System.IO.StreamReader sr = new System.IO.StreamReader(ofd.FileName))
                {
                    var text = sr.ReadToEnd();
                    var dic = JsonUtility.JsonParser.GetAllValuesAsDic(text);
                    var msg = string.Join(Environment.NewLine,
                        dic.Select(x => string.Format("{0}={1}", x.Key, x.Value)).Take(
                        dic.Keys.Count > 30 ? 30 : dic.Keys.Count));
                    MessageBox.Show(msg);


                    /** necessary to add Newtonsoft.Json */
                    var json = Newtonsoft.Json.JsonConvert.DeserializeObject(text);
                    //var json = JsonUtility.JsonParser.Deserialize(text);

                    var dic2 = JsonUtility.JsonParser.GetAllValuesAsDic(json);
                    var msg2 = string.Join(Environment.NewLine,
                        dic2.Select(x => string.Format("{0}={1}", x.Key, x.Value)).Take(
                        dic2.Keys.Count > 30 ? 30 : dic2.Keys.Count));
                    MessageBox.Show(msg2);

                }

                //Open as binary
                using (System.IO.FileStream fs = new System.IO.FileStream(ofd.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read))
                {
                    var binaryData = new byte[fs.Length];
                    fs.Read(binaryData, 0, binaryData.Length);

                    var dic1 = JsonUtility.JsonParser.GetAllValuesAsDic(binaryData);
                    var msg1 = string.Join(Environment.NewLine,
                        dic1.Select(x => string.Format("{0}={1}", x.Key, x.Value)).Take(
                        dic1.Keys.Count > 30 ? 30 : dic1.Keys.Count));
                    MessageBox.Show(msg1, "Open as binary");
                }
            }

        };

        Button buttonCreate = new Button() { Text = "Create", Dock = DockStyle.Top, Parent = this, };
        buttonCreate.Click += (sender, e) =>
        {
            /* Want to create below
               {"name":"foo", "age":"50", "children":{"name":"bar", "age":"20", "gender":"female","hobby":["tennis","swimming","walking"]}}
            */

            /** Create JObject. Same as 
            var jobj = JsonUtility.JsonCreator.CreateEmptyJObject();
            JsonUtility.JsonCreator.AddOrUpdateValue(jobj, "name", "foo");
            Or
            var jobj= new Newtonsoft.Json.Linq.JObject();
            */

            var jobj = JsonUtility.JsonCreator.CreateJObject(
                new KeyValuePair<string, object>("name", "foo"));

            JsonUtility.JsonCreator.AddOrUpdateValue(jobj, "age", "50");

            /** Create JArray */
            var jarrayHobby = JsonUtility.JsonCreator.CreateJArray();
            jarrayHobby.Add("tennis");
            jarrayHobby.Add("swimming");
            jarrayHobby.Add("walking");

            var jobjChildren = JsonUtility.JsonCreator.CreateEmptyJObject();
            JsonUtility.JsonCreator.AddOrUpdateValue(jobjChildren, "name","bar");
            JsonUtility.JsonCreator.AddOrUpdateValue(jobjChildren, "age", "200");
            /** Whoops! */
            JsonUtility.JsonCreator.AddOrUpdateValue(jobjChildren, "age", "20");
            JsonUtility.JsonCreator.AddOrUpdateValue(jobjChildren, "gender", "female");
            JsonUtility.JsonCreator.AddOrUpdateValue(jobjChildren, "hobby", jarrayHobby);
            JsonUtility.JsonCreator.AddOrUpdateValue(jobj, "children", jobjChildren);

            var dic = JsonUtility.JsonParser.GetAllValuesAsDic(jobj);
            var msg = string.Join(Environment.NewLine,
                dic.Select(x => string.Format("{0}={1}", x.Key, x.Value)).Take(
                dic.Keys.Count > 30 ? 30 : dic.Keys.Count));

            MessageBox.Show(msg);

            var dstfn = string.Format(".\\{0:yyyyMMdd_HHmmss}_json.csv", DateTime.Now);
            using (System.IO.StreamWriter sw = new System.IO.StreamWriter(dstfn, false, Encoding.UTF8))
            {
                sw.Write(JsonUtility.JsonParser.Serialize(jobj));
            }

            MessageBox.Show(string.Format("Save as {0}", dstfn) );

        };

        StartPosition = FormStartPosition.CenterScreen;

    }
}
Product Versions
.NET Framework net452 net46 net461 net462 net463 net47 net471 net472 net48
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.0.1 66 9/9/2022
1.1.0 216 4/9/2022
1.0.7 314 10/13/2021