Skip to main content

JsonDocumentConverter

Namespace: Wisej.Web.Ext.ChartJS4.Serialization

Assembly: Wisej.Web.Ext.ChartJS4 (4.1.0.0)

Converts JsonDocument or JsonElement to a plain object structure suitable for Wisej serialization. This avoids the "ValueKind: Object" issue when deserializing to ExpandoObject.

public static class JsonDocumentConverter

JSON objects become Dictionary<string, object>, arrays become List<object>, and strings, numbers and booleans become the corresponding .NET primitives. ChartJS4 uses these helpers to serialize ChartOptions and strip default and empty values before sending the options to the client.

Example:

using var doc = JsonDocument.Parse("{\"plugins\":{\"legend\":{}},\"responsive\":true}");
var obj = JsonDocumentConverter.ConvertToObject(doc);
obj = JsonDocumentConverter.RemoveEmptyObjects(obj);
// obj is a Dictionary<string, object> containing only "responsive" = true

Methods​

Static member ConvertElement(element)​

Converts a JsonElement to a plain object structure (Dictionary/List/primitives).

ParameterTypeDescription
elementJsonElementThe JsonElement to convert.

Returns: Object. A plain object structure that can be serialized by Wisej.

Objects are converted to Dictionary<string, object> and arrays to List<object>, recursively. Numbers are returned as Int32 when they fit, otherwise as Int64, otherwise as Double. null and undefined values are returned as null.

Example:

using var doc = JsonDocument.Parse("[1, 2.5, \"a\", true]");
var list = (List<object>)JsonDocumentConverter.ConvertElement(doc.RootElement);
// list contains 1 (int), 2.5 (double), "a" (string), true (bool)

Static member ConvertToObject(document)​

Converts a JsonDocument to a plain object structure (Dictionary/List/primitives).

ParameterTypeDescription
documentJsonDocumentThe JsonDocument to convert.

Returns: Object. A plain object structure that can be serialized by Wisej, or null if document is null.

Converts the RootElement using ConvertElement.

Example:

using var doc = JsonDocument.Parse("{\"type\":\"bar\",\"data\":[1,2,3]}");
var obj = (Dictionary<string, object>)JsonDocumentConverter.ConvertToObject(doc);
var type = (string)obj["type"]; // "bar"

Static member RemoveDefaultValues(obj, sourceType)​

Removes properties that match their DefaultValue attribute from a dictionary structure. This handles custom default values defined via [DefaultValue] attributes that System.Text.Json doesn't automatically filter (it only filters CLR default values).

ParameterTypeDescription
objObjectThe object to clean (Dictionary, List, or primitive).
sourceTypeTypeThe type of the object being serialized (used to look up DefaultValue attributes).

Returns: Object. The cleaned object with default values removed.

Dictionary keys are matched to the public instance properties of sourceType using the camelCase property name (case-insensitive). Entries whose value equals the property's DefaultValueAttribute are removed; numeric values are compared as Double. Nested dictionaries are processed recursively using the type of the matching property.

Items of lists are processed without type information, so their default values are not removed. If obj or sourceType is null, obj is returned unchanged.

Example:

var doc = JsonSerializer.SerializeToDocument(chart.ChartOptions);
var obj = JsonDocumentConverter.ConvertToObject(doc);
obj = JsonDocumentConverter.RemoveDefaultValues(obj, typeof(ChartOptions));

Static member RemoveEmptyObjects(obj)​

Removes empty objects and null values from a dictionary structure recursively. This helps reduce payload size by removing unnecessary empty objects.

ParameterTypeDescription
objObjectThe object to clean (Dictionary, List, or primitive).

Returns: Object. The cleaned object, or null if it became empty.

Dictionaries are cleaned in place: entries whose value is null or an empty dictionary (after cleaning) are removed. Lists are replaced by a new list without null items. Empty dictionaries and empty lists are returned as null. Primitive values are returned unchanged.

Example:

var options = new Dictionary<string, object>
{
["plugins"] = new Dictionary<string, object> { ["legend"] = new Dictionary<string, object>() },
["responsive"] = true
};
var cleaned = JsonDocumentConverter.RemoveEmptyObjects(options); // only "responsive" remains