Skip to content

Serialization ​

JASE.Core includes lightweight serializers for JSON, CBOR, and XML.

They are designed for production code where you want:

  • explicit defaults (no “magic” settings that drift),
  • predictable output,
  • and low friction APIs for common cases.

Note: “Deterministic” here means JASE chooses and documents defaults explicitly. It does not guarantee byte-for-byte identical output across all runtimes and versions for every format (property ordering is the classic edge case).

JSON ​

JSON serialization is implemented via System.Text.Json and exposed through JASE.Serializers.JSON.

Defaults ​

JSON.GetOptions() returns a cached JsonSerializerOptions instance that is rebuilt when you change settings.

Current defaults configured by JASE:

  • AllowTrailingCommas = true
  • DefaultIgnoreCondition = WhenWritingNull (IgnoreNulls defaults to true)
  • PropertyNamingPolicy = JsonNamingPolicy.CamelCase (CamelCaseProperties defaults to true)
  • WriteIndented = PrettyPrint (PrettyPrint defaults to false)
  • Always adds JsonStringEnumConverter (enums serialize as strings)

Optional converters you can enable:

  • AlphabetizeProperties = false → JsonConverters.AlphabeticalOrderConverter
  • GuidNoHyphen → adds JsonConverters.GuidNoHyphenConverter
  • SuppressEmptyGuid → adds JsonConverters.EmptyGuidConverter

Settings ​

These are global settings on the JSON class:

  • JSON.SetIgnoreNulls(bool)
  • JSON.SetCamelCaseProperties(bool)
  • JSON.SetPrettyPrint(bool)
  • JSON.SetGuidNoHyphen(bool)
  • JSON.SetSuppressEmptyGuid(bool)

There is an ErrorOnMissingMembers flag exposed, but there is currently no setter and System.Text.Json does not directly support “error on unknown/missing members” in the same way Newtonsoft did.

Serialize / Deserialize ​

csharp
using JASE.Serializers;

string json = JSON.Serialize(new { name = "Ada", value = 123 });

MyType obj = (MyType)JSON.Deserialize(json, typeof(MyType));

You can also pass an explicit JsonSerializerOptions to Serialize(...) / Deserialize(...) if you don’t want the global settings.

UTF-8 bytes ​

csharp
using JASE.Serializers;

byte[] bytes = JSON.SerializeToUtf8Bytes(myObject);

Prettify and uncomment ​

If you receive JSON that includes comments (non-standard JSON), JASE includes helpers:

csharp
using JASE.Serializers;

string cleaned = JSON.UncommentJson(inputJson);
string pretty  = JSON.Prettify(cleaned);

Merge ​

JSON.Merge(original, transformJson) merges JSON structures at the DOM level (object/object merge, array handling, scalar replacement). Use this when you want a controlled overlay rather than “deserialize, mutate, reserialize”.

CBOR ​

CBOR serialization is implemented via System.Formats.Cbor and exposed through JASE.Serializers.Cbor.

CBOR is useful when you control both ends of the wire and want smaller payloads and faster encode/decode than text JSON.

Supported type model ​

Cbor.Serialize(object) writes CBOR using the following rules:

  • null → CBOR null
  • byte[] → CBOR byte string
  • IDictionary → CBOR map (string keys only; non-string keys throw)
  • IEnumerable (except string) → CBOR array
  • primitives: int, uint, long, ulong, bool, double, float
  • string → CBOR text string
  • Enum → encoded via internal enum logic
  • DateTime → written as an ISO-8601 string using DateTime.ToISO8601(CoreConfig.IsoDatePrecision)
  • DateTimeOffset → written via CborWriter.WriteDateTimeOffset

For “plain objects” (POCOs), CBOR uses reflection and writes a CBOR map of public instance properties:

  • includes properties that are readable (CanRead)
  • ignores properties marked with:
  • JASE.Serializers.Attributes.CborIgnoreAttribute
  • System.Text.Json.Serialization.JsonIgnoreAttribute
  • uses JsonPropertyNameAttribute if present to determine the serialized field name; otherwise uses the property name

Serialize ​

csharp
using JASE.Serializers;

byte[] bytes = Cbor.Serialize(myObject);

Deserialize ​

CBOR deserialization returns a logical object model composed of:

  • Dictionary<string, object> for maps
  • List<object> for arrays
  • primitives / strings / byte[] as appropriate
csharp
using JASE.Serializers;

object obj = Cbor.Deserialize(bytes);

There is a typed helper, but it is strict:

csharp
using JASE.Serializers;

MyType typed = Cbor.Deserialize<MyType>(bytes);

If the decoded object is not assignable to MyType, it throws. (This is intentional: no guessing/casting that hides schema mismatches.)

Determinism notes for CBOR ​

  • Map property order for reflected POCOs follows reflection enumeration order, which should be treated as not guaranteed across runtimes/versions.
  • Keys are not automatically camel-cased; use [JsonPropertyName("...")] if you need a stable external contract.
  • DateTime is written as ISO-8601 text; if you need a binary time encoding contract, you should define it explicitly in your data model.

XML ​

XML serialization is exposed through JASE.Serializers.Xml.

This is intended for interoperability scenarios (legacy integrations, SOAP-ish payloads, vendor APIs) where XML is non-negotiable.

Serialize / Deserialize ​

csharp

using JASE.Serializers;

string xml = Xml.Serialize(myObject, omitDeclaration: true, prettify: true);
MyType obj = (MyType)Xml.Deserialize(xml, typeof(MyType));

Aliases exist:

  • Xml.ToXml(...) → same behavior as Serialize(...)
  • Xml.FromXML(...) → same behavior as Deserialize(...)

Options supported on serialization include:

  • omitDeclaration
  • prettify
  • newLineOnAttributes
  • optional namespaces map

XML validation and formatting ​

csharp

using JASE.Serializers;

bool ok = Xml.Validate(xmlText);
string pretty = Xml.Prettify(xmlText, omitDeclaration: true);

XML querying helpers ​

If you need DOM-style manipulation and selection:

  • Xml.GetXDocument(...)
  • Xml.ToXDocument(...)
  • Xml.FindXElement(...)
  • Xml.SelectXElement(...)
  • Xml.IsXElementNil(...)

SOAP Helpers ​

For SOAP-like payloads, Xml includes helpers that pull out method names and parameters:

  • SoapGetMethodName(...)
  • SoapGetMethodElement(...)
  • SoapGetMethodParameter(...)
  • SoapGetMethodParameterValue(...)