Json.JsonDocument

WTSupported in traditional Synergy on Windows
WNSupported in Synergy .NET on Windows
USupported on UNIX
VSupported on OpenVMS
namespace Json
public class JsonDocument

The JsonDocument class provides a way to examine the structural content of a JSON value without automatically instantiating data values.

Methods

Parse

public static Parse(json), @JsonDocument

Parses the text in json, which is a string or memory handle (@string or D_HANDLE), into a JSON document.

Properties

public RootElement, @JsonElement

Gets the root element of this JsonDocument.

Discussion

JsonDocument acts as a single reference point for all of its associated JsonElement objects. All resulting JsonElement objects that come from this JsonDocument, its properties, and its method are only valid while the JsonDocument is alive and valid. JsonElement objects do not maintain a reference count back to their parent JsonDocument and will not be valid if the JsonDocument is destroyed.

JsonDocument reduces the burden of garbage collection by using resources from pooled memory. If you don’t dispose of this object properly, memory won’t be returned to the pool, which will negatively affect performance.

Example

import System.Text
import System.Text.Json

.define SAMPLE_FILE, "C:\temp\mysample.json"

; There is definitely no requirement to make these properties defines/const,
; but always a good idea to reduce your magic strings in code
.region "Json Property Strings"
.define JSON_NAMEPROP, "Name"
.define JSON_SAMPLEARRAYPROP, "SampleArray"
.define JSON_IDPROP, "Id"
.define JSON_BOOLEANDATAPROP, "BooleanData"
.define JSON_STRINGDATAPROP, "StringData"
.endregion

;;; <summary>
;;; Sample program that demonstrates first writing some Json data,
;;; then reading that data back in and displaying it to the console.
;;; </summary>
main

proc
    JsonSample.WriteSampleData(SAMPLE_FILE)
    JsonSample.ReadSampleData(SAMPLE_FILE)
    Console.ReadLine()
endmain

namespace JsonSample
    class JsonSample

      ;;; <summary>
      ;;; Method demonstrating using Utf8JsonWriter to output a JSON file
      ;;; </summary>
      public static method WriteSampleData, void
          filename, string
          endparams
      proc
          begin
            data jsonBuffer = new StringBuilder()
            data jsonWriter = Utf8JsonWriter.CreateUtf8JsonWriter(jsonBuffer)
            data writeChannel, i4
            data iterator, i4
            data stringData = "Hello World Hello World Hello World"

            jsonWriter.WriteStartObject()
            jsonWriter.WriteString(JSON_NAMEPROP, "MySampleObject")
            jsonWriter.WriteStartArray(JSON_SAMPLEARRAYPROP)
            for iterator from 1 thru 10 by 1
            begin
              jsonWriter.WriteStartObject()
              jsonWriter.WriteNumber(JSON_IDPROP, iterator)
              jsonWriter.WriteBoolean(JSON_BOOLEANDATAPROP, GetRandomBool())
              jsonWriter.WriteString(JSON_STRINGDATAPROP, stringData(iterator:11))
              jsonWriter.WriteEndObject()
            end
            jsonWriter.WriteEndArray()
            jsonWriter.WriteEndObject()
            jsonWriter.Flush()
            jsonWriter.Reset()

            open(writeChannel = 0, O, filename)
            writes(writeChannel, jsonBuffer.ToString())
            close writeChannel
          end
      endmethod

      ;;; <summary>
      ;;; Demonstrate the basic API for getting information out of a JsonDocument and JsonElements
      ;;;
      ;;; IMPORTANT NOTE:
      ;;;   The JsonDocument object is the lifeblood of the rest of the objects. If it gets
      ;;;   disposed or the scope ends, all of the child JsonElement objects will also be
      ;;;   disposed. If you need them to leave the scope intact, make sure you keep the
      ;;;   JsonDocument object alive
      ;;; </summary>
      public static method ReadSampleData, void
          filename, string
          endparams
      proc
          begin
            data jsDoc, @JsonDocument
            data myElement, @JsonElement
            data elementName, @JsonElement
            data elementArray, @JsonElement
            data arrayLength, i4
            data arrayIterator, i4

            jsDoc = Parse(filename)
            myElement = jsDoc.RootElement
            elementName = myElement.GetProperty(JSON_NAMEPROP)
            elementArray = myElement.GetProperty(JSON_SAMPLEARRAYPROP)
            arrayLength = elementArray.GetArrayLength()
            for arrayIterator from 0 thru arrayLength-1 by 1
            begin
              data itemId = elementArray[arrayIterator].GetProperty(JSON_IDPROP).GetInt32()
              data boolData = elementArray[arrayIterator].GetProperty(JSON_BOOLEANDATAPROP).GetBoolean()
              data stringData = elementArray[arrayIterator].GetProperty(JSON_STRINGDATAPROP).GetString()
              Console.WriteLine("Read in item " + %string(itemId) + " [" + %string(boolData) + ", " + stringData + "]")
            end
          end
      endmethod

      ;;; <summary>
      ;;; Get a "random" boolean. Just to add flavor to the sample data.
      ;;; </summary>
      ;;; <returns>true or false, pseudo-randomly</returns>
      private static method GetRandomBool, boolean
          endparams
          record
              number, int
          endrecord
      proc
          number = Utilities.GetRandomNumber()
          mreturn %rnd(number) > 16383
      endmethod

    ;;; <summary>
    ;;; Utility method to read in a file from disk and return that file
    ;;; content parsed through JsonDocument.Parse
    ;;; </summary>
    ;;; <param name="filePath"></param>
    ;;; <returns></returns>
    public static method Parse, @JsonDocument
        req in filePath, string
        endparams

        record
          jsonFileContent, @StringBuilder
          firstBrace, int
          channel, int
          buffer, a1024
          plays,string
        endrecord

        proc
            jsonFileContent = new StringBuilder()
            open(channel = 0, I, filePath)

            while (true)
            begin
              reads(channel, buffer, eof)
              if (firstBrace < 1)
              begin
                xcall instr(1, buffer, '{', firstBrace)
                if (firstBrace >= 1)
                  buffer = buffer(firstBrace:1024-firstBrace)
              end
              jsonFileContent.Append(%atrim(buffer))
              nextloop
          eof,
              jsonFileContent.Append(%atrim(buffer))
              exitloop
          end
              close(channel)
              plays=jsonFileContent.ToString()
          mreturn JsonDocument.Parse(jsonFileContent.ToString())

        endmethod

    endclass

.region "Utilities"
    static class Utilities
        ;;; Stores the seed used by GetRandomNumber
        private static randomSeed, int, 1
        ;;; <summary>
        ;;; Synergy randm() does not work particularly well when hit with the same
        ;;; seeds in the same millisecond/second. This method tracks its own random
        ;;; seed and produces a random number based on seed and time.
        ;;; </summary>
        ;;; <returns></returns>
        public static method GetRandomNumber, int
            endparams
            record
                number, int
                ctime, int
            endrecord
        proc
            xcall time(ctime)
            xcall randm(randomSeed, randomSeed, ctime)
            xcall randm(number, randomSeed, ctime)
            mreturn number
        endmethod
    endclass
.endregion
endnamespace