JsonSerialization Class |
Namespace: Novacta.Analytics
The JsonSerialization type exposes the following members.
Name | Description | |
---|---|---|
AddDataConverters |
Adds support to the JSON conversion of data types in the
Novacta.Analytics namespace.
|
This class provides custom converters for the JSON serialization classes that are defined in the System.Text.Json namespace.
In particular, method AddDataConverters(JsonSerializerOptions) adds to a given JsonSerializerOptions instance the converters required for the JSON serialization of the following types: DoubleMatrix, ReadOnlyDoubleMatrix, ComplexMatrix, ReadOnlyComplexMatrix, Category, CategoricalVariable, CategoricalEntailment, and CategoricalDataSet.
In the following example, a matrix instance is serialized by writing to a JSON value. Hence the matrix is deserialized by reading from such value.
using System; using System.Text.Json; namespace Novacta.Analytics.CodeExamples { public class JsonSerializationExample0 { public void Main() { // Set matrix dimensions. const int numberOfRows = 3; const int numberOfColumns = 2; // Create the data as an array having lower bounds equal to zero. var data = new double[numberOfRows, numberOfColumns] { { 1, 2 }, { 3, 4 }, { 5, 6 } }; // Create the matrix. var serializedMatrix = DoubleMatrix.Dense(data); // Set the matrix name. serializedMatrix.Name = "MyData"; // Set names for some rows and columns. serializedMatrix.SetRowName(0, "Row-0"); serializedMatrix.SetRowName(2, "Row-2"); serializedMatrix.SetColumnName(1, "Col-0"); // Show the matrix. Console.WriteLine("Serialized matrix:"); Console.WriteLine(serializedMatrix); Console.WriteLine(); // Add converters to a JsonSerializerOptions instance // to support the JSON serialization of data types // defined in the Novacta.Analytics namespace. var options = new JsonSerializerOptions { WriteIndented = true }; JsonSerialization.AddDataConverters(options); // Create a JSON representation of the matrix. // The options previously defined must be passed as a parameter // to method Serialize of class System.Text.Json.JsonSerializer. string json = JsonSerializer.Serialize( serializedMatrix, typeof(DoubleMatrix), options); // Show the JSON value representing the matrix. Console.WriteLine("JSON matrix representation:"); Console.WriteLine(json); Console.WriteLine(); // Create a matrix from the JSON representation. // The options previously defined must be passed as a parameter // to method Deserialize of class System.Text.Json.JsonSerializer. var deserializedMatrix = JsonSerializer.Deserialize<DoubleMatrix>( json, options); // Show the deserialized matrix. Console.WriteLine("Deserialized matrix:"); Console.WriteLine(deserializedMatrix); } } } // Executing method Main() produces the following output: // // Serialized matrix: // [Col-0] // [Row-0] 1 2 // 3 4 // [Row-2] 5 6 // // // // JSON matrix representation: // { // "Implementor": { // "StorageScheme": 0, // "NumberOfRows": 3, // "NumberOfColumns": 2, // "Storage": [ // 1, // 3, // 5, // 2, // 4, // 6 // ] // }, // "Name": "MyData", // "RowNames": { // "0": "Row-0", // "2": "Row-2" // }, // "ColumnNames": { // "1": "Col-0" // } // } // // Deserialized matrix: // [Col-0] // [Row-0] 1 2 // 3 4 // [Row-2] 5 6 //
In the following example, a categorical data set is converted to a JSON value. Hence the data set is instantiated by reading from such value.
using System; using System.Collections.Generic; using System.Text.Json; namespace Novacta.Analytics.CodeExamples { public class JsonSerializationExample1 { public void Main() { // Create the feature variables. CategoricalVariable f0 = new("F-0") { { 0, "A" }, { 1, "B" }, { 2, "C" }, { 3, "D" }, { 4, "E" } }; f0.SetAsReadOnly(); CategoricalVariable f1 = new("F-1") { { 0, "I" }, { 1, "II" }, { 2, "III" }, { 3, "IV" } }; f1.SetAsReadOnly(); // Create the response variable. CategoricalVariable r = new("R") { 0, 1, 2 }; r.SetAsReadOnly(); // Create a categorical data set containing // observations about such variables. List<CategoricalVariable> variables = new() { f0, f1, r }; DoubleMatrix data = DoubleMatrix.Dense( new double[8, 3] { { 0, 0, 0 }, { 0, 1, 0 }, { 1, 2, 2 }, { 1, 3, 2 }, { 2, 0, 1 }, { 3, 1, 1 }, { 4, 2, 1 }, { 4, 3, 1 } }); CategoricalDataSet serializedDataSet = CategoricalDataSet.FromEncodedData( variables, data); // Set the data set name. serializedDataSet.Name = "MyData"; // Add converters to a JsonSerializerOptions instance // to support the JSON serialization of data types // defined in the Novacta.Analytics namespace. var options = new JsonSerializerOptions { WriteIndented = true }; JsonSerialization.AddDataConverters(options); // Create a JSON representation of the data set. // The options previously defined must be passed as a parameter // to method Serialize of class System.Text.Json.JsonSerializer. string json = JsonSerializer.Serialize( serializedDataSet, typeof(CategoricalDataSet), options); // Show the JSON value representing the data set. Console.WriteLine("JSON data set representation:"); Console.WriteLine(json); Console.WriteLine(); // Create a data set from the JSON representation. // The options previously defined must be passed as a parameter // to method Deserialize of class System.Text.Json.JsonSerializer. var deserializedDataSet = JsonSerializer.Deserialize<CategoricalDataSet>( json, options); // Show the deserialized data set name. Console.WriteLine("Deserialized data set name:"); Console.WriteLine(deserializedDataSet.Name); } } } // Executing method Main() produces the following output: // // JSON data set representation: // { // "Variables": [ // { // "Name": "F-0", // "Categories": [ // { // "Code": 0, // "Label": "A" // }, // { // "Code": 1, // "Label": "B" // }, // { // "Code": 2, // "Label": "C" // }, // { // "Code": 3, // "Label": "D" // }, // { // "Code": 4, // "Label": "E" // } // ], // "IsReadOnly": true // }, // { // "Name": "F-1", // "Categories": [ // { // "Code": 0, // "Label": "I" // }, // { // "Code": 1, // "Label": "II" // }, // { // "Code": 2, // "Label": "III" // }, // { // "Code": 3, // "Label": "IV" // } // ], // "IsReadOnly": true // }, // { // "Name": "R", // "Categories": [ // { // "Code": 0, // "Label": "0" // }, // { // "Code": 1, // "Label": "1" // }, // { // "Code": 2, // "Label": "2" // } // ], // "IsReadOnly": true // } // ], // "Data": { // "Matrix": { // "Implementor": { // "StorageScheme": 0, // "NumberOfRows": 8, // "NumberOfColumns": 3, // "Storage": [ // 0, // 0, // 1, // 1, // 2, // 3, // 4, // 4, // 0, // 1, // 2, // 3, // 0, // 1, // 2, // 3, // 0, // 0, // 2, // 2, // 1, // 1, // 1, // 1 // ] // }, // "Name": "MyData", // "RowNames": null, // "ColumnNames": { // "0": "F-0", // "1": "F-1", // "2": "R" // } // } // }, // "Name": "MyData" // } // // Deserialized data set name: // MyData