Skip to content

Instantly share code, notes, and snippets.

@pedrosimoes79
Created April 12, 2019 18:00
Show Gist options
  • Save pedrosimoes79/0064abe522a59584c5856e3c46936032 to your computer and use it in GitHub Desktop.
Save pedrosimoes79/0064abe522a59584c5856e3c46936032 to your computer and use it in GitHub Desktop.
using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Base58Check;
/// <summary>
/// dotnet add package Newtonsoft.Json
/// dotnet add package Base58Check (in netcore there will be some warnings but it will work fine)
/// </summary>
namespace μJSON
{
public class Program
{
public class ConverterContractResolver : DefaultContractResolver
{
public static readonly ConverterContractResolver Instance = new ConverterContractResolver();
private static readonly IDictionary<Type, IDictionary<string, string>> mapping = new Dictionary<Type, IDictionary<string, string>>();
protected override IList<JsonProperty> CreateProperties(Type t, MemberSerialization ms)
{
var props = base.CreateProperties(t, ms).OrderBy(p => p.PropertyName).ToList();
if(!mapping.ContainsKey(t)) {
var m = new Dictionary<string, string>();
var names = new HashSet<string>();
foreach(var prop in props) {
if(!m.ContainsKey(prop.PropertyName)) {
using (var sha1 = SHA256.Create())
{
var hash = Base58CheckEncoding.EncodePlain(sha1.ComputeHash(Encoding.UTF8.GetBytes(prop.PropertyName)));
var name = string.Empty;
foreach(var c in hash) {
name += c;
if(!names.Contains(name)) break;
}
m[prop.PropertyName] = name;
names.Add(name);
}
}
}
mapping[t] = m;
}
foreach(var prop in props) {
if(mapping.ContainsKey(t) && mapping[t].ContainsKey(prop.PropertyName)) {
prop.PropertyName = mapping[prop.DeclaringType][prop.PropertyName];
}
}
return props;
}
}
private static readonly JsonSerializer serializer = new JsonSerializer() {
ContractResolver = ConverterContractResolver.Instance,
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Ignore,
};
public static string Encode<T>(T obj)
where T : class, new()
{
using(var sw = new StringWriter())
{
using (var jtw = new JsonTextWriter(sw))
{
jtw.QuoteName = false;
serializer.Serialize(jtw, obj);
}
return sw.ToString();
}
}
public static T Decode<T>(string input)
where T : class, new()
{
using(var tr = new StringReader(input))
{
using(var jtr = new JsonTextReader(tr))
{
return serializer.Deserialize<T>(jtr);
}
}
}
public class Transaction
{
/// <summary>
/// Transaction Id
/// </summary>
[JsonProperty("TransactionID")]
public Guid TransactionId { get; set; }
/// <summary>
/// Transaction date
/// </summary>
public DateTime TransactionDate { get; set; }
/// <summary>
/// Store Id
/// </summary>
[JsonProperty("StoreCode")]
public string StoreId { get; set; }
/// <summary>
/// Fiscal Id
/// </summary>
[JsonProperty("Nif")]
public string FiscalId { get; set; }
/// <summary>
/// Customer card
/// </summary>
public string CustomerCard { get; set; }
/// <summary>
/// Payment method
/// </summary>
public string PaymentMethod { get; set; }
/// <summary>
/// Token code
/// </summary>
public string TokenCode { get; set; }
/// <summary>
/// Detail line count
/// </summary>
public int LineCount { get; set; }
/// <summary>
/// Transanction detail lines
/// </summary>
[JsonProperty("TransactionsDetails")]
public IEnumerable<TransactionDetail> TransactionDetails { get; set; }
}
public class TransactionDetail
{
/// <summary>
/// Product barcode
/// </summary>
public string Barcode { get; set; }
}
static void Main(string[] args)
{
var json = @"
{
""TransactionID"": ""c85ae1a0-6fde-44f5-beb2-f357b3e35778"",
""TransactionDate"": ""2019-04-10T15:55:55.097"",
""StoreCode"": ""025"",
""Nif"": ""205006760"",
""CustomerCard"": ""6006189300000019479"",
""PaymentMethod"": ""023"",
""TokenCode"": ""1102517555208"",
""LineCount"": 13,
""TransactionsDetails"": [{
""Barcode"": ""5601002093622""
}, {
""Barcode"": ""5601002093622""
}, {
""Barcode"": ""5601002093622""
}, {
""Barcode"": ""5601002093622""
}, {
""Barcode"": ""5601002093622""
}, {
""Barcode"": ""5601002093622""
}, {
""Barcode"": ""5601002093622""
}, {
""Barcode"": ""5601002093622""
}, {
""Barcode"": ""5601002093622""
}, {
""Barcode"": ""5601002093622""
}, {
""Barcode"": ""5601002093622""
}, {
""Barcode"": ""5601002093622""
}, {
""Barcode"": ""5601002093622""
}
]
}";
var t = JsonConvert.DeserializeObject<Transaction>(json);
Console.WriteLine(JsonConvert.SerializeObject(t));
var s = Encode(t);
Console.WriteLine(s);
t = Decode<Transaction>(s);
Console.WriteLine(Encode(t));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment