Demo - Paranoid-AF/Jsona GitHub Wiki

Demo

Code

#include "Jsona/Jsona"
void PluginInit(){
  g_Module.ScriptInfo.SetAuthor("Paranoid_AF");
  g_Module.ScriptInfo.SetContactInfo("https://github.com/Paranoid-AF");
  string test = "{\"date\":\"20191223\",\"articles\":[{\"id\":12,\"title\":\"We're out of beta!\",\"category\":0},{\"id\":13,\"title\":\"The cake is a lie!\",\"category\":1}]}";
  g_Game.AlertMessage(at_console, test+"\n");
  
  // parse JSON string
  Jsona::Value@ val = Jsona::parse(test);
  
  // get data straight out of Value
  dictionary@ root = dictionary(val);
  
  // add new key to object, with bi-directional data flow (by making use of reference).
  root["homepage"] = Jsona::Value("https://github.com/Paranoid-AF");
  root["age"] = Jsona::Value(19);
  
  // directly manipulate Value
  Jsona::Value@ valInner = val["date"];
  if(valInner.type() == Jsona::STRING_VALUE){
    valInner.set(string(valInner) + " but whatever.");
  }
  
  // directly manipulate Value, with bi-directional data flow (by making use of reference).
  array<Jsona::Value@>@ arr = array<Jsona::Value@>(val["articles"]);
  arr.insertLast(Jsona::Value("Hola!"));
  arr[2].set("Hello");
  
  // operator overloads, always use string for index, even if it's an array.
  val["articles"]["0"]["title"] = Jsona::Value("Actually, no.");
  
  // output
  g_Game.AlertMessage(at_console, string(val["articles"]["1"]["title"])+"\n");
  g_Game.AlertMessage(at_console, Jsona::stringify(val)+"\n");
}

Original Value

{
  "date":"20191223",
  "articles":
  [
    {
      "id":12,
      "title":"We're out of beta!",
      "category":0
      },
    {
      "id":13,
      "title":"The cake is a lie!",
      "category":1
    }
  ]
}

Output (stylised)

The cake is a lie!
{
  "age":19,
  "articles":[
    {
      "category":0,
      "id":12,
      "title":"Actually, no."
    },
    {
      "category":1,
      "id":13,
      "title":"The cake is a lie!"
    },
    "Hello"
  ],
  "date":"20191223 but whatever.",
  "homepage":"https://github.com/Paranoid-AF"
}