JSON (JavaScript Object Notation) is a popular text-based
serialization format. In contrast to other text-based formats
that store tables, JSON can represent arbitrary objects.
The JSON functionality in Numerics.NET
is built on the
JSON.NET
framework. Both stand-alone classes and classes that integrate with
JSON.NET serialization are available.
The classes that implement the JSON functionality live in the
Numerics.NET.Data.Json namespace.
Because of the dependency on JSON.NET, they were given their own assembly,
Numerics.NET.Data.Json.dll.
The JsonFile
object, which is the File-like
class for reading and writing JSON, has the same functionality as all
other File-like clases.
The data source can be provided either as a string or a stream.
The string can be the path to a file on the local file system, or
it may point to a web resource.
In addition, the Open
method can take a NewtonSoft.Json.JsonReader, and the
Append
method can take a NewtonSoft.Json.JsonWriter as the data source.
All methods take an optional argument of type JsonOptions
that gives some additional details on how to read or write the JSON.
In addition to the standard methods, there are also methods that can convert
a data frame, matrix, or vector to a JSON string or vice versa.
To convert an object to a JSON
string, use one of the overloads of the
ToJson
method. To deserialize an object from a string in JSON format, use the
ToVector<T>(String, JsonOptions),
ToMatrix<T>(String, JsonOptions),
or ToDataFrame<R, C>(String, JsonOptions)
method.
These methods are defined as extension methods, so they can be called on the objects
and strings directly. The example below constructs a data frame, serializes it
to a string, and then immediately deserializes the string to end up
with an identical data frame:
var data = new Dictionary<string, object>() {
{ "state", new string[] { "Ohio", "Ohio", "Ohio", "Nevada", "Nevada" } },
{ "year", new int[] { 2000, 2001, 2002, 2001, 2002 } },
{ "pop", new double[] { 1.5, 1.7, 3.6, 2.4, 2.9 } }
};
var df1 = DataFrame.FromColumns(data);
var jsonString = df1.ToJson();
var df2 = jsonString.ToDataFrame();
Dim data = New Dictionary(Of String, Object)() From {
{ "state", { "Ohio", "Ohio", "Ohio", "Nevada", "Nevada" } },
{ "year", { 2000, 2001, 2002, 2001, 2002 } },
{ "pop", { 1.5, 1.7, 3.6, 2.4, 2.9 } } }
Dim df1 = DataFrame.FromColumns(data)
Dim jsonString = df1.ToJson()
Dim df2 = jsonString.ToDataFrame()
No code example is currently available or this language may not be supported.
let columns1 : IVector[] =
[|
Vector.Create(11.0, 14.0, 17.0, 93.0, 55.0) ;
Vector.Create(22.0, 33.0, 43.0, 51.0, 69.0)
|]
let df1 = DataFrame.FromColumns(columns1,
Index.Create([| "First"; "Second" |]))
let jsonString = df1.ToJson()
let df2 = jsonString.ToDataFrame()
The JSON.NET library provides a full framework for serializing and
deserializing any kind of .NET object. This is done most commonly
through the use of serialization attributes which are applied to
classes and their members to specify the exact format of the serialization.
To avoid a direct dependency of our core assembly on the JSON.NET assembly,
we use a different mechanism: converters. A converter class is a class that
can convert between some types of objects and text in JSON format.
Two converters are available. The ComplexJsonConverter
class converts complex numbers of any type.
The DataObjectJsonConverter
class can convert data frames, matrices, vectors, and indexes.
These converters can be used as the argument of a JsonConverter
attribute to specify that a field should be serialized and deserialized using this converter.
Below is a complete sample that defines an object, complete with JSON
serialization attributes. We then create an instance, serialize it
to a string, and then deserialize to get a copy of the original object:
using System.IO;
using Numerics.NET.Data.Json;
using Numerics.NET.DataAnalysis;
using Numerics.NET;
using Newtonsoft.Json;
[JsonObject]
public class Instrument
{
public string Symbol { get; set; }
[JsonConverter(typeof(DataObjectJsonConverter))]
public Vector<double> History { get; set; }
}
class SerializationExample
{
public static void Sample()
{
var instrument = new Instrument();
instrument.Symbol = "APPL";
instrument.History = Vector.Create(1.0, 2.0, 3.0, 4.0, 5.0);
instrument.History.Index = Index.CreateDateRange(new DateTime(2016, 05, 1), 5, Recurrence.Daily);
var s = new JsonSerializer();
var sw = new StringWriter();
s.Serialize(sw, instrument);
var json = sw.ToString();
Console.WriteLine(json);
s.Converters.Add(new DataObjectJsonConverter());
var sr = new StringReader(json);
var instrument2 = s.Deserialize<Instrument>(new JsonTextReader(sr));
Console.WriteLine(instrument2.Symbol);
Console.WriteLine(instrument2.History);
}
}
Imports System.IO
Imports Numerics.NET.Data.Json
Imports Numerics.NET.DataAnalysis
Imports Numerics.NET
Imports Newtonsoft.Json
<JsonObject>
Public Class Instrument
Public Property Symbol As String
<JsonConverter(GetType(DataObjectJsonConverter))>
Public Property History As Vector(Of Double)
End Class
Class SerializationExample
Public Shared Sub Sample()
Dim instrument = New Instrument()
instrument.Symbol = "APPL"
instrument.History = Vector.Create(1.0, 2.0, 3.0, 4.0, 5.0)
instrument.History.Index = Index.CreateDateRange(New DateTime(2016, 5, 1), 5, Recurrence.Daily)
Dim s = New JsonSerializer()
Dim sw = New StringWriter()
s.Serialize(sw, instrument)
Dim json = sw.ToString()
Console.WriteLine(json)
s.Converters.Add(New DataObjectJsonConverter())
Dim sr = New StringReader(json)
Dim instrument2 = s.Deserialize(Of Instrument)(New JsonTextReader(sr))
Console.WriteLine(instrument2.Symbol)
Console.WriteLine(instrument2.History)
End Sub
End Class
Class DataAccess
Public Shared Sub Json()
Dim data = New Dictionary(Of String, Object)() From {
{ "state", { "Ohio", "Ohio", "Ohio", "Nevada", "Nevada" } },
{ "year", { 2000, 2001, 2002, 2001, 2002 } },
{ "pop", { 1.5, 1.7, 3.6, 2.4, 2.9 } } }
Dim df1 = DataFrame.FromColumns(data)
Dim jsonString = df1.ToJson()
Dim df2 = jsonString.ToDataFrame()
End Sub
Public Shared Sub RFile()
Dim df1 = RdataFile.ReadDataFrame(Of DateTime, String)("c:\data.rda")
Dim df2 = RdataFile.ReadDataFrame("www.example.com/sample.rda", "frame")
Dim vector1 = RdataFile.ReadVector(Of Double)("c:\vector.rda")
Dim matrix1 = RdataFile.ReadMatrix(Of Double)("www.example.com/matrix.rda", "mat")
Dim dataFrameVars = { "frame1", "frame2" }
Dim dataFrames = RdataFile.ReadDataFrames("c:\data.rda", dataFrameVars)
Dim frame1 = dataFrames("frame1")
Dim frame2 = dataFrames("frame2")
RdataFile.Write("c:\data.rda", df1)
Dim Stream = File.OpenWrite("c:\output.rda")
RdataFile.Write(Stream, matrix1)
End Sub
Public Shared Sub RStream()
Using s1 = RdataFile.Open("www.example.com/sample.rda")
Dim df1 = s1.ReadDataFrame(Of DateTime, String)()
Dim df2 = s1.ReadDataFrame("frame")
End Using
Using s2 = RdataFile.Open("c:\vector.rda")
Dim vector1 = s2.ReadVector(Of Double)()
Dim matrix1 = s2.ReadMatrix(Of Double)("mat")
End Using
Using s2 = RdataFile.Open("c:\sample.rda")
Dim dataFrameVars = { "frame1", "frame2" }
Dim dataFrames = s2.ReadDataFrames(dataFrameVars)
Dim frame1 = dataFrames("frame1")
Dim frame2 = dataFrames("frame2")
End Using
End Sub
Public Shared Sub RStream2()
Dim frame1 As DataFrame(Of Long, String) = Nothing
Dim frame2 As DataFrame(Of Long, String) = Nothing
Dim matrix1 As Matrix(Of Double) = Nothing
Dim vector1 As Vector(Of Double) = Nothing
Using stream = RdataFile.Create("c:\sample.rda")
stream.Write({ frame1, frame2 }, { "df1", "df2" })
stream.Write(matrix1, "matrix1")
stream.Write(vector1, "vector1")
End Using
End Sub
Public Shared Sub MatlabFiles()
Dim vector1 = MatlabFile.ReadVector(Of Double)("c:\vector.mat")
Dim matrix1 = MatlabFile.ReadMatrix(Of Double)("www.example.com/matrix.mat", "mat")
End Sub
Public Shared Sub MatlabStreams()
Using s2 = MatlabFile.Open("c:\sample.mat")
Dim vector1 = s2.ReadVector(Of Double)()
Dim matrix1 = s2.ReadMatrix(Of Double)("a")
End Using
Using s2 = MatlabFile.Open("c:\sample.mat")
Dim dataFrameVars = { "frame1", "frame2" }
Dim dataFrames = s2.ReadDataFrames(dataFrameVars)
Dim frame1 = dataFrames("frame1")
Dim frame2 = dataFrames("frame2")
End Using
End Sub
Public Shared Sub MatlabStreams2()
End Sub
Public Shared Sub StataFiles()
Dim df1 = StataFile.ReadDataFrame(Of DateTime, String)("c:\data.dta")
Dim df2 = StataFile.ReadDataFrame("www.example.com/sample.dta")
End Sub
Public Shared Sub StataStreams()
Using s1 = RdataFile.Open("www.example.com/sample.dta")
Dim df1 = s1.ReadDataFrame(Of DateTime, String)()
End Using
End Sub
Public Shared Sub FixedWidthFile()
Dim columnBreaks = { 0, 10, 20, 30 }
Dim options = New FixedWidthTextOptions(columnBreaks)
Dim df1 = FixedWidthTextFile.ReadDataFrame(Of DateTime, String)(
"c:\data.txt", options)
Dim df2 = FixedWidthTextFile.ReadDataFrame(
"http://www.example.com/sample.txt", columnBreaks)
Dim vector1 = FixedWidthTextFile.ReadVector(Of Double)(
"c:\vector.txt", options)
Dim complexBreaks = { 0, 10, 20, 30, 40, 50 }
Dim matrix1 = FixedWidthTextFile.ReadComplexMatrix(Of Double)(
"http://www.example.com/matrix.txt", complexBreaks)
End Sub
Public Shared Sub FixedWidthStream()
Dim options = New FixedWidthTextOptions({ 0, 10, 20, 30 })
Using s1 = FixedWidthTextFile.Open("http://www.example.com/sample.txt", options)
Dim df1 = s1.ReadDataFrame(Of DateTime, String)()
End Using
Dim breaks = { 0, 10 }
Using s2 = FixedWidthTextFile.Open("c:\vector.txt", breaks)
Dim vector1 = s2.ReadComplexVector(Of Double)()
End Using
End Sub
Public Shared Sub DFile()
Dim df1 = DelimitedTextFile.ReadDataFrame(Of DateTime, String)("c:\data.csv")
Dim df2 = DelimitedTextFile.ReadDataFrame(
"http://www.example.com/sample.tsv", DelimitedTextOptions.Tsv)
Dim vector1 = DelimitedTextFile.ReadVector(Of Double)("c:\vector.csv")
Dim culture = CultureInfo.GetCultureInfo("de-DE")
Dim options = DelimitedTextOptions.CsvForCulture(culture)
Dim matrix1 = DelimitedTextFile.ReadMatrix(Of Double)(
"http://www.example.com/german.csv", options)
DelimitedTextFile.Write("c:\data.csv", df1)
Using stream = File.OpenWrite("c:\output.csv")
DelimitedTextFile.Write(stream, matrix1)
End Using
End Sub
Public Shared Sub DStream()
Using s1 = DelimitedTextFile.Open("http://www.example.com/sample.csv")
Dim df1 = s1.ReadDataFrame(Of DateTime, String)()
End Using
Using s2 = DelimitedTextFile.Open("c:\vector.csv")
Dim vector1 = s2.ReadVector(Of Double)()
End Using
Dim matrix1 As Matrix(Of Double) = Nothing
Using stream = DelimitedTextFile.Create("c:\data.csv")
stream.Write(matrix1)
End Using
End Sub
Public Shared Sub MMFile()
Dim vector1 = MatrixMarketFile.ReadVector(Of Double)("c:\vector.mtx")
Dim matrix1 = MatrixMarketFile.ReadMatrix(Of Double)(
"http://www.example.com/german.mtx")
MatrixMarketFile.Write("c:\data.mtx", vector1)
Using stream = File.OpenWrite("c:\output.mtx")
MatrixMarketFile.Write(stream, matrix1)
End Using
End Sub
Public Shared Sub MMStream()
Using s1 = MatrixMarketFile.Open("http://www.example.com/sample.mtx")
Dim df1 = s1.ReadDataFrame(Of DateTime, String)()
End Using
Using s2 = MatrixMarketFile.Open("c:\vector.mtx")
Dim vector1 = s2.ReadVector(Of Double)()
End Using
Dim matrix1 As Matrix(Of Double) = Nothing
Using stream = MatrixMarketFile.Create("c:\data.mtx")
stream.Write(matrix1)
End Using
No code example is currently available or this language may not be supported.
open System.IO
open Numerics.NET.Data.Json
open Numerics.NET.DataAnalysis
open Numerics.NET
open Newtonsoft.Json
[<JsonObject>]
type Instrument =
{
Symbol : string;
[<JsonConverter(typeof<DataObjectJsonConverter>)>] History : Vector<float>
}
module SerializationExample =
let Sample =
let instrument =
{
Symbol = "APPL";
History = Vector.Create(1.0, 2.0, 3.0, 4.0, 5.0)
}
instrument.History.Index <-
Index.CreateDateRange(new DateTime(2016, 05, 1), 5, Recurrence.Daily);
let s = new JsonSerializer()
let sw = new StringWriter()
s.Serialize(sw, instrument)
let json = sw.ToString()
printfn "%s" json
s.Converters.Add(new DataObjectJsonConverter())
let sr = new StringReader(json)
let instrument2 = s.Deserialize<Instrument>(new JsonTextReader(sr))
printfn "%s" (instrument2.Symbol)
printfn "%O" (instrument2.History)