Skip to content

Instantly share code, notes, and snippets.

@t0chas
Last active November 30, 2016 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t0chas/af5bc562d3fe411af4e2f1aced153301 to your computer and use it in GitHub Desktop.
Save t0chas/af5bc562d3fe411af4e2f1aced153301 to your computer and use it in GitHub Desktop.
Json.Net.Unity3D struct example
[System.Serializable]
public struct ProductItems
{
public static readonly ProductItems Default = default(ProductItems);
public string ProductId { get; set; }
public int Count { get; set; }
public ProductItems(string productId, int count)
{
this.ProductId = productId;
this.Count = count;
}
public override string ToString()
{
return string.Format("{0}x{1}", this.ProductId, this.Count);
}
public static ProductItems Parse(string json)
{
return ParseJson(json);
}
public static ProductItems ParseJson(string json)
{
string[] arr = json.Split('x');
if (arr == null || arr.Length != 2)
return Default;
string productId = arr[0];
int count = 0;
int.TryParse(arr[1], out count);
ProductItems item = new ProductItems(productId, count);
return item;
}
public static ProductItems operator *(ProductItems item, int scalar)
{
item.Count = item.Count * scalar;
return item;
}
public static IEnumerable<ProductItems> Multiply(IEnumerable<ProductItems> items, int scalar)
{
List<ProductItems> ret = new List<ProductItems>();
foreach(var item in items)
{
ret.Add(item * scalar);
}
return ret;
}
public static ProductItems[] Multiply(ProductItems[] items, int scalar)
{
ProductItems[] ret = new ProductItems[items.Length];
for(int i = 0; i < items.Length; i++)
{
ret[i] = items[i] * scalar;
}
return ret;
}
}
using UnityEngine;
using System.Collections;
using Newtonsoft.Json;
using SmallFactories.Model;
public class ProductItemsConverter : JsonConverter {
public override bool CanConvert (System.Type objectType)
{
return objectType == typeof(ProductItems);
}
public override bool CanRead {
get {
return true;
}
}
public override bool CanWrite {
get {
return true;
}
}
public override object ReadJson (JsonReader reader, System.Type objectType, object existingValue, JsonSerializer serializer)
{
//Debug.LogFormat ("ReadJson {0}; {1}", objectType.Name, existingValue);
do {
var value = (string)reader.Value;
//Debug.LogFormat ("whileRead Token: '{0}'; Value: '{1}';", reader.TokenType, value);
if (reader.TokenType != JsonToken.String) {
break;
}
string[] arr = value.Split(new char[]{'x'}, 2);
if(arr.Length == 2){
int n = 0;
int.TryParse(arr[1], out n);
ProductItems items = new ProductItems(arr[0], n);
return items;
}
return default(ProductItems);
//Debug.LogFormat ("read value: {0};", propertyName);
/*if (!reader.Read())
continue;*/
} while (reader.Read ());
//Debug.LogFormat ("end read loop");
return default(ProductItems);
}
public override void WriteJson (JsonWriter writer, object value, JsonSerializer serializer)
{
throw new System.NotImplementedException ();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment