ObjectGenerator.NET
1.0.3.5
dotnet add package ObjectGenerator.NET --version 1.0.3.5
NuGet\Install-Package ObjectGenerator.NET -Version 1.0.3.5
<PackageReference Include="ObjectGenerator.NET" Version="1.0.3.5" />
paket add ObjectGenerator.NET --version 1.0.3.5
#r "nuget: ObjectGenerator.NET, 1.0.3.5"
// Install ObjectGenerator.NET as a Cake Addin
#addin nuget:?package=ObjectGenerator.NET&version=1.0.3.5
// Install ObjectGenerator.NET as a Cake Tool
#tool nuget:?package=ObjectGenerator.NET&version=1.0.3.5
The fastest way to Generate Strongly Typed Objects
from valid JSON
, without using reflection.
ObjectGenerator<T>
contains specialized methods for deserializing objects that meet certain criteria.
Using a Generator
to deserialize is about 40% faster than a certain generalized library.
This README
explains the entire concept.
Generating Objects
Objects can be generated by using an ObjectGenerator
and providing it with Type Information
This section doubles as a full example on how to create a Generator
Generate Single Object
Provide a New Object
and Type Information
to the Generator
to create Testable
string singleTestdata = "{" +
"\"testInt\":1234567890," +
"\"testString\":\"The quick brown fox, jumps over; the lazy dog and ^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$|^(bc1)[0-9A-Za-z]{39,59}$\"," +
"\"testDecimal\":\"1234.000000\"," +
"\"testLong\":234556667777788888," +
"\"testArray\":[[\"BTC1\",\"701.90000000\",\"713.90000000\"],[\"BTC2\",\"702.90000000\",\"723.90000000\"]]," +
"\"testObject\":{[\"BTC1\",\"701.90000000\",\"713.90000000\"],[\"BTC2\",\"702.90000000\",\"723.90000000\"]}," +
"\"testBoolean\":true" +
"}";
Testable singleTest = new Testable(); // New Object (gets filled with data)
ObjectGenerator<Testable>.Generate(ref singleTest, ref singleTestdata, TestableTypeInformation); // See below
Generating List of Object
Provide a New Object
and Type Information
and a method create a New Object
to the Generator
to create List<Testable>
static Func<Testable> NewObjectMethodTestable = () => new Testable(); // New Object Method
string doubleTestData = "[" +
"{" +
"\"testInt\":1234567890," +
"\"testString\":\"The quick brown fox, jumps over; the lazy dog and ^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$|^(bc1)[0-9A-Za-z]{39,59}$\"," +
"\"testDecimal\":\"1234.000000\"," +
"\"testLong\":234556667777788888," +
"\"testBoolean\":true," +
"\"testArray\":[[\"BTC1\",\"701.90000000\",\"713.90000000\"],[\"BTC2\",\"702.90000000\",\"723.90000000\"]]," +
"\"testObject\":{[\"BTC1\",\"701.90000000\",\"713.90000000\"],[\"BTC2\",\"702.90000000\",\"723.90000000\"]}," +
"}," +
"{" +
"\"testInt\":1234567890," +
"\"testString\":\"The quick brown fox, jumps over; the lazy dog and ^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$|^(bc1)[0-9A-Za-z]{39,59}$\"," +
"\"testDecimal\":\"1234.000000\"," +
"\"testLong\":234556667777788888," +
"\"testBoolean\":true," +
"\"testArray\":[[\"BTC1\",\"701.90000000\",\"713.90000000\"],[\"BTC2\",\"702.90000000\",\"723.90000000\"]]," +
"\"testObject\":{[\"BTC1\",\"701.90000000\",\"713.90000000\"],[\"BTC2\",\"702.90000000\",\"723.90000000\"]}," +
"}" +
"]";
List<Testable> doubleTest = new List<Testable>(); // New Object (gets filled with data)
ObjectGenerator<Testable>.GenerateEnumerable(ref NewObjectMethodTestable, ref doubleTest, ref doubleTestData, TestableTypeInformation); // See below
All GenerateEnumerable
methods create List<T>
, including GenerateEnumerableArray
Providing New Objects
When using a Generator
to create List<T>
you will need to provide a method to create a new object
This can be as simple as the examples below or any method that returns the correct type and matches the signature
static Func<Testable> NewObjectMethodTestable = () => new Testable();
static Func<TestableArray> NewObjectMethodArray = () => new TestableArray();
Creating a Generator
To generate an object you need to provide information about the type and how the members will be generated (example below and above)
These can have references to other Generators
public static void TestableTypeInformation(Testable obj, string valueName, string value)
{
switch (valueName)
{
case "testInt":
{
obj.TestInt = int.Parse(value);
break;
}
case "testString":
{
obj.TestString = value.Trim('"');
break;
}
case "testDecimal":
{
obj.TestDecimal = decimal.Parse(value.Trim('"'));
break;
}
case "testBoolean":
{
obj.TestBoolean = bool.Parse(value);
break;
}
case "testLong":
{
obj.TestLong = long.Parse(value);
break;
}
case "testArray":
{
List<TestableArray> array = new List<TestableArray>();
ObjectGenerator<TestableArray>.GenerateEnumerableArray(ref NewObjectMethodArray, ref array, ref value, ArrayTypeInformation);
obj.TestArray = array;
break;
}
case "testObject":
{
List<TestableArray> array = new List<TestableArray>();
ObjectGenerator<TestableArray>.GenerateEnumerableArray(ref NewObjectMethodArray, ref array, ref value, ArrayTypeInformation);
obj.TestObject = array;
break;
}
}
}
Array methods need their information provided a different way, the members should be in the expected order (example below)
public static void ArrayTypeInformation(TestableArray obj, int position, string value)
{
switch (position)
{
case 0:
{
obj.Symbol = value.Trim('"');
break;
}
case 1:
{
obj.Price = decimal.Parse(value.Trim('"'));
break;
}
case 2:
{
obj.Price2 = decimal.Parse(value.Trim('"'));
break;
}
}
}
Some Array methods take convertable type information like so (example below)
internal static ExampleType ConvertTypeInformation(string at)
{
return at == "SPOT" ? ExampleType.Spot :
at == "MARGIN" ? ExampleType.Margin :
at == "FUTURES" ? ExampleType.Futures :
at == "LEVERAGED" ? ExampleType.Leveraged :
at == "TRD_GRP_002" ? ExampleType.TRD_GRP_002 :
ExampleType.Unknown;
}
Generator Examples
These examples are for demonstration purposes to show the kinds of data each generator can be used for, these are taken from the tests
Other Generators
may be added from time to time these were all made for specific purposes
GenerateSimple
Ignores data structure and attempts to create a single ExampleType
from the data
string websocketData = "{\"stream\":\"bchusdt@miniTicker\",\"data\":{\"e\":\"24hrMiniTicker\",\"E\":1712541696355,\"s\":\"BCHUSDT\",\"c\":\"697.90000000\",\"o\":\"694.00000000\",\"h\":\"701.40000000\",\"l\":\"674.70000000\",\"v\":\"153874.79800000\",\"q\":\"105541200.83290000\"}}";
ExampleType singleTick = new();
ObjectGenerator<ExampleType>.GenerateSimple(ref singleTick, ref websocketData, ExampleTypeCreateInternal);
GenerateSimpleIndex
Ignores data structure and attempts to create a single ExampleType
from the data, starting at a specific index
string websocketData = "{\"stream\":\"bchusdt@miniTicker\",\"data\":{\"e\":\"24hrMiniTicker\",\"E\":1712541696355,\"s\":\"BCHUSDT\",\"c\":\"697.90000000\",\"o\":\"694.00000000\",\"h\":\"701.40000000\",\"l\":\"674.70000000\",\"v\":\"153874.79800000\",\"q\":\"105541200.83290000\"}}";
ExampleType singleTick = new();
ObjectGenerator<ExampleType>.GenerateSimpleIndex(ref singleTick, ref websocketData, ExampleTypeCreateInternal, 60);
GenerateEnumerableIndex
Creates a List<ExampleType>
from the data, starting at the index of the first object that is relevant
List<ExampleType> singleTest = new();
string streamDataEnumerable = "{\"stream\":\"!miniTicker@arr\",\"data\":[{\"e\":\"24hrMiniTicker\",\"E\":1713144460261,\"s\":\"BTCUSDT\",\"c\":\"65454.89000000\",\"o\":\"63808.68000000\",\"h\":\"65867.45000000\",\"l\":\"62134.00000000\",\"v\":\"58245.21800000\",\"q\":\"3738086445.09890780\"}," +
"{\"e\":\"24hrMiniTicker\",\"E\":1713144460289,\"s\":\"ETHUSDT\",\"c\":\"3119.13000000\",\"o\":\"2984.95000000\",\"h\":\"3174.23000000\",\"l\":\"2912.41000000\",\"v\":\"698464.93230000\",\"q\":\"2134202672.13823200\"}," +
"{\"e\":\"24hrMiniTicker\",\"E\":1713144460213,\"s\":\"XRPBTC\",\"c\":\"0.00000761\",\"o\":\"0.00000743\",\"h\":\"0.00000775\",\"l\":\"0.00000737\",\"v\":\"45247362.00000000\",\"q\":\"343.34460526\"}," +
"{\"e\":\"24hrMiniTicker\",\"E\":1713144460764,\"s\":\"BNBUSDT\",\"c\":\"562.90000000\",\"o\":\"547.60000000\",\"h\":\"576.00000000\",\"l\":\"536.10000000\",\"v\":\"870394.69100000\",\"q\":\"481822418.82950000\"}]}";
ObjectGenerator<ExampleType>.GenerateEnumerableIndex(ref NewObjectMethodExampleType, ref singleTest, ref streamDataEnumerable, ExampleTypeCreateInternal, 57);
GenerateEnumerableArray
Creates a List<TestableArray>
from an array of objects
string websocketData = "[[\"1499040000000\",\"0.01634790\",\"0.80000000\"]]";
List<TestableArray> websocketSingleTest = new();
ObjectGenerator<TestableArray>.GenerateEnumerableArray(ref NewObjectMethodArray, ref websocketSingleTest, ref websocketData, ArrayTypeInformation);
GenerateEnumerableArray2D
Creates a List<List<ExampleType>>
from an array of an array of enums
string restData = "[" +
"[\"SPOT\",\"MARGIN\",\"TRD_GRP_004\",\"TRD_GRP_005\",\"TRD_GRP_006\"]," +
"[\"SPOT\",\"MARGIN\",\"TRD_GRP_004\",\"TRD_GRP_005\",\"TRD_GRP_006\"]," +
"[\"SPOT\",\"MARGIN\",\"TRD_GRP_004\",\"TRD_GRP_005\",\"TRD_GRP_006\"]" +
"]";
List<List<ExampleType>> websocketSingleTest = new();
ObjectGenerator<ExampleType>.GenerateEnumerableArray2D(ConvertTypeInformation, ref websocketSingleTest, ref restData, ref NewListObjectMethodArray);
GenerateSpecificMember
Creates Testable
from the specific member result
, ignoring everything else
string singleTestData = "{" +
"\"id\":\"123\"," +
"\"status\":200," +
"\"result\":" +
"{" +
"\"testInt\":1234567890," +
"\"testDecimal\":\"1234.000000\"," +
"\"testString\":\"The quick brown fox, jumps over; the lazy dog and ^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$|^(bc1)[0-9A-Za-z]{39,59}$\"," +
"\"testLong\":234556667777788888," +
"\"testArray\":[[\"BTC1\",\"701.90000000\",\"713.90000000\"],[\"BTC2\",\"702.90000000\",\"723.90000000\"]]," +
"\"testObject\":{[\"BTC1\",\"701.90000000\",\"713.90000000\"],[\"BTC2\",\"702.90000000\",\"723.90000000\"]}," +
"\"testBoolean\":true" +
"}" +
"}";
Testable specificMemberTest = new();
ObjectGenerator<Testable>.GenerateSpecificMember("result", ref specificMemberTest, ref singleTestData, TestableTypeInformation);
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on ObjectGenerator.NET:
Package | Downloads |
---|---|
BinanceObjects
Methods for turning raw responses from the RestAPI and WebsocketAPI into Binance Objects |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.3.5 | 135 | 5/7/2024 |
README
Package Tags
-----------------
Please use the most recent version visible on Nuget, old versions are hidden but can still be downloaded indirectly