mirror of
https://github.com/esiur/esiur-dotnet.git
synced 2025-06-26 21:13:13 +00:00
Added JSON
This commit is contained in:
@ -36,6 +36,7 @@ using System.Reflection;
|
||||
using Esiur.Resource.Template;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Collections;
|
||||
using System.Dynamic;
|
||||
|
||||
namespace Esiur.Data
|
||||
{
|
||||
@ -239,7 +240,8 @@ namespace Esiur.Data
|
||||
/// <param name="includeTypes">Whether to include each item DataType</param>
|
||||
/// <param name="prependLength">If true, prepend the length as UInt32 at the beginning of the returned bytes array</param>
|
||||
/// <returns>Array of bytes in the network byte order</returns>
|
||||
public static byte[] ComposeStructure(Structure value, DistributedConnection connection, bool includeKeys = true, bool includeTypes = true, bool prependLength = false)
|
||||
public static byte[] ComposeStructure(Structure value, DistributedConnection connection,
|
||||
bool includeKeys = true, bool includeTypes = true, bool prependLength = false)
|
||||
{
|
||||
var rt = new BinaryList();
|
||||
|
||||
@ -1269,7 +1271,7 @@ namespace Esiur.Data
|
||||
type = DataType.String;
|
||||
else if (t == typeof(DateTime))
|
||||
type = DataType.DateTime;
|
||||
else if (typeof(Structure).IsAssignableFrom(t))
|
||||
else if (typeof(Structure).IsAssignableFrom(t) || t == typeof(ExpandoObject))
|
||||
type = DataType.Structure;
|
||||
//else if (t == typeof(DistributedResource))
|
||||
// type = DataType.DistributedResource;
|
||||
|
@ -533,16 +533,22 @@ namespace Esiur.Data
|
||||
|
||||
public static string ToHex(this byte[] ba, uint offset, uint length, string separator = " ")
|
||||
{
|
||||
StringBuilder hex = new StringBuilder((int)length * 2);
|
||||
|
||||
for (var i = offset; i < offset + length; i++)
|
||||
{
|
||||
hex.AppendFormat("{0:x2}", ba[i]);
|
||||
if (separator != null)
|
||||
hex.Append(separator);
|
||||
}
|
||||
if (separator == null)
|
||||
separator = "";
|
||||
|
||||
return hex.ToString();
|
||||
return string.Join(separator, ba.Skip((int)offset).Take((int)length).Select(x => x.ToString("x2")).ToArray());
|
||||
|
||||
//StringBuilder hex = new StringBuilder((int)length * 2);
|
||||
|
||||
//for (var i = offset; i < offset + length; i++)
|
||||
//{
|
||||
// hex.AppendFormat("{0:x2}", ba[i]);
|
||||
// if (separator != null)
|
||||
// hex.Append(separator);
|
||||
//}
|
||||
|
||||
//return hex.ToString();
|
||||
}
|
||||
|
||||
public static byte[] FromHex(string hexString, string separator = " ")
|
||||
|
91
Esiur/Data/ResourceJsonConverter.cs
Normal file
91
Esiur/Data/ResourceJsonConverter.cs
Normal file
@ -0,0 +1,91 @@
|
||||
using Esiur.Resource;
|
||||
/*
|
||||
|
||||
Copyright (c) 2017-2021 Ahmed Kh. Zamil
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Esiur.Data
|
||||
{
|
||||
class ResourceJsonConverter : JsonConverter<IResource>
|
||||
{
|
||||
public override IResource Read(
|
||||
ref Utf8JsonReader reader,
|
||||
Type typeToConvert,
|
||||
JsonSerializerOptions options)
|
||||
{
|
||||
return (IResource)JsonSerializer.Deserialize(ref reader,typeof(IResource), options);
|
||||
}
|
||||
|
||||
|
||||
public override void Write(
|
||||
Utf8JsonWriter writer,
|
||||
IResource resource,
|
||||
JsonSerializerOptions options)
|
||||
{
|
||||
|
||||
writer.WriteStartObject();
|
||||
|
||||
foreach (var pt in resource.Instance.Template.Properties)
|
||||
{
|
||||
var rt = pt.Info.GetValue(resource, null);
|
||||
writer.WritePropertyName(options.PropertyNamingPolicy?.ConvertName(pt.Name) ?? pt.Name);
|
||||
JsonSerializer.Serialize(writer, rt, options);
|
||||
}
|
||||
|
||||
writer.WriteEndObject();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class DoubleJsonConverter : JsonConverter<double>
|
||||
{
|
||||
public override double Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
if (reader.TokenType == JsonTokenType.String && reader.GetString() == "NaN")
|
||||
{
|
||||
return double.NaN;
|
||||
}
|
||||
|
||||
return reader.GetDouble(); // JsonException thrown if reader.TokenType != JsonTokenType.Number
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, double value, JsonSerializerOptions options)
|
||||
{
|
||||
if (double.IsNaN(value))
|
||||
{
|
||||
writer.WriteStringValue("NaN");
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.WriteNumberValue(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -85,6 +85,8 @@ namespace Esiur.Data
|
||||
rt.dic = source.dic;
|
||||
return rt;
|
||||
}
|
||||
|
||||
public static explicit operator Structure(ExpandoObject obj) => FromDynamic(obj);
|
||||
|
||||
public static Structure FromDynamic(ExpandoObject obj)
|
||||
{
|
||||
|
Reference in New Issue
Block a user