Json
JSON parsing and output with explicit result and data-schema checks.
Create Json, call Parse and use Data only after Error.Ok. Separately validate root type, fields and permitted values. The parser accepts trailing commas; it is not strict compatibility validation for every external JSON consumer.
When reusing an instance, diagnostics and retained text can remain from a previous call. The returned Error determines success. ParseString is shorter, but its Nil cannot distinguish failure from JSON null.
JSON numbers parse as Float. Store large identifiers in an agreed string format, or use FromNative/ToNative encoding for supported engine types. Ordinary Stringify does not automatically preserve Vector3, Resource or a C# class.
The example reads an integer score field from 0 to 1000000. Additional fields are allowed; syntax, root, type, finiteness and range are checked before returning the result.
using System;
using Reckfell;
public static class JsonScoreExample
{
public static bool TryReadScore(string text, out int score)
{
score = 0;
using var document = new Json();
if (document.Parse(text) != Error.Ok)
return false;
using Variant root = document.Data;
if (root.VariantType != Variant.Type.Dictionary)
return false;
using var fields = root.AsReckfellDictionary();
bool found = fields.TryGetValue("score", out Variant value);
using (value)
{
if (!found || value.VariantType != Variant.Type.Float)
return false;
double number = value.AsDouble();
if (!double.IsFinite(number) || number < 0 || number > 1_000_000
|| number != Math.Truncate(number))
return false;
score = (int)number;
return true;
}
}
}See saving games for a complete persistence workflow.
Declarations checked against C# SDK 1.0.0. Expand a name to see declarations and documentation.
Constructors1
JsonConstructor
public Json();Creates an instance with Nil Data. Prefer a fresh instance for independent parsing: repeated Parse does not guarantee clearing previous data, retained text and diagnostics on every failure path.
Properties1
DataProperty
public Variant Data { get; set; }Parsed or explicitly assigned Variant value. Read a Parse result only after Error.Ok: failure can retain an old value, while another failure path produces Nil. Assigning Data clears GetParsedText(), but does not parse text or reset every diagnostic.
Methods10
FromNativeVariant
public static Variant FromNative(Variant variant, bool fullObjects = false);Encodes supported engine types into JSON-compatible values using its own tags, prefixes and structure. Returns a Variant, not text: call Stringify afterward if needed. This is the FromNative/ToNative pair protocol, not an ordinary JSON DTO.
| Name | Type | Default |
|---|---|---|
variant | Variant | — |
fullObjects | bool | false |
variantEngine value to encode. Not an arbitrary managed C# object; support follows Variant types.
fullObjectsDefaults to false: Object encoding is disallowed and can return Nil with a diagnostic. true enables supported object-property serialization, not a copy of all application state.
VariantEncoded Variant; unsupported/disallowed paths can produce Nil with diagnostics. Nil also encodes an original Nil.
GetDataVariant
public Variant GetData();Returns Data. Contained Array/Dictionary values remain shared rather than becoming an independent deep clone. Dispose the returned Variant after use.
VariantVariant from Data. Check the latest Parse Error before interpreting it.
GetErrorLineint
public int GetErrorLine();Returns the diagnostic line index from Parse. Counting starts at 0; success also sets 0, so inspect Error rather than using the line number to determine success.
intZero-based line index; 0 does not distinguish success from a first-line failure.
GetErrorMessagestring
public string GetErrorMessage();Returns the stored parse diagnostic. Read it when Parse fails. In the current implementation, a later successful Parse can retain an old message; a nonempty string alone does not mean the latest call failed.
stringStored diagnostic, which can refer to an earlier failure.
GetParsedTextstring
public string GetParsedText();Returns the last text supplied to Parse with keepText: true, including failed input. keepText: false does not clear previously retained text. Assigning Data/SetData clears it. This is not a fresh serialization of Data.
stringRetained input text, or an empty string when none. May not correspond to current Data.
ParseError
public Error Parse(string jsonText, bool keepText = false);Parses text and returns Error. After Error.Ok, validate root type, required fields and ranges. The parser accepts some extensions such as trailing commas; it is not a strict format validator for every external system. Do not use Data after failure. A repeated object key retains the last value.
| Name | Type | Default |
|---|---|---|
jsonText | string | — |
keepText | bool | false |
jsonTextComplete input text. The parser checks the document end but accepts some nonstrict syntax; successful parsing does not validate game requirements.
keepTexttrue retains the input string even on failure. false leaves a previously retained string intact; it is not a clear command.
ErrorError.Ok when input is accepted; otherwise a failure code. Use this as the success indicator, rather than GetErrorMessage or Data.
ParseStringVariant
public static Variant ParseString(string jsonString);Shorthand parsing without a separate Error. Valid JSON null and failure both return Nil; failure also logs a native diagnostic. Use an instance Json.Parse to distinguish them.
| Name | Type | Default |
|---|---|---|
jsonString | string | — |
jsonStringJSON text. Use instance Parse instead to obtain a separate Error and diagnostic.
VariantParsed Variant. Nil does not distinguish failure from valid JSON null.
SetDatavoid
public void SetData(Variant data);Assigns Data and clears retained source text. Does not read JSON, validate a schema or automatically clear all information from a previous Parse.
| Name | Type | Default |
|---|---|---|
data | Variant | — |
dataOperation input. For Stringify, follow your JSON schema; for SetData, a direct Variant value, not text to parse.
Stringifystring
public static string Stringify(Variant data, string indent = "", bool sortKeys = true, bool fullPrecision = false);Converts a Variant to JSON text without writing a file. An integer Variant emits a decimal integer, but Parse reads JSON numbers as Float: large integers can lose precision. For ordinary JSON, define a schema, use string keys and finite numbers. Not complete serialization of arbitrary engine objects.
| Name | Type | Default |
|---|---|---|
data | Variant | — |
indent | string | "" |
sortKeys | bool | true |
fullPrecision | bool | false |
dataOperation input. For Stringify, follow your JSON schema; for SetData, a direct Variant value, not text to parse.
indentIndentation per level. Empty produces compact output; use spaces or tabs for readable JSON. Arbitrary non-whitespace characters are inserted literally and can invalidate JSON.
sortKeysSort dictionary keys before output; defaults to true. Use string keys for external JSON. Sorting does not establish a universal canonical format.
fullPrecisionUse fuller Float formatting for exact double reading. Does not fix large-Int precision loss during later Parse or add standard JSON NaN/Infinity support.
stringGenerated text. NaN becomes null with a warning; Infinity becomes signed 1e99999. Not a portable round trip for nonfinite values; validate finiteness before writing.
ToNativeVariant
public static Variant ToNative(Variant json, bool allowObjects = false);Decodes a representation produced by FromNative. For text input, call Parse and check Error first. Do not treat arbitrary ordinary JSON as the encoded representation. Enabling allowObjects can create objects and assign properties/scripts: not schema validation or a sandbox.
| Name | Type | Default |
|---|---|---|
json | Variant | — |
allowObjects | bool | false |
jsonJSON-compatible Variant in FromNative format, not a JSON string to parse directly.
allowObjectsDefaults to false. true permits object creation during decoding; use only trusted encoded input with controlled types.
VariantRestored engine value; Nil can indicate failure or a valid original Nil.