1
0
mirror of https://github.com/Raymonf/whack.git synced 2025-02-25 21:58:02 +01:00
whack/WTT/UAssetAPI/JSON/TMapJsonConverter.cs
2022-09-28 18:39:41 -04:00

81 lines
3.0 KiB
C#

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Specialized;
/*
The code in this file is modified from mattmc3's dotmore @ https://github.com/mattmc3/dotmore/tree/b032bbf871d46bffd698c9b7a233c533d9d2f0ebs for usage in UAssetAPI.
The MIT License (MIT)
Copyright (c) 2014 mattmc3
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 UAssetAPI.UnrealTypes;
namespace UAssetAPI.JSON
{
public class TMapJsonConverter<TKey, TValue> : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return typeof(IOrderedDictionary).IsAssignableFrom(objectType);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
ICollection keys = ((IOrderedDictionary)value).Keys;
ICollection values = ((IOrderedDictionary)value).Values;
IEnumerator valueEnumerator = values.GetEnumerator();
writer.WriteStartArray();
foreach (object key in keys)
{
valueEnumerator.MoveNext();
writer.WriteStartArray();
serializer.Serialize(writer, key);
serializer.Serialize(writer, valueEnumerator.Current);
writer.WriteEndArray();
}
writer.WriteEndArray();
}
public override bool CanRead
{
get { return true; }
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var dictionary = new TMap<TKey, TValue>();
JToken tokens = JToken.Load(reader);
foreach (var eachToken in tokens)
{
TKey key = eachToken[0].ToObject<TKey>(serializer);
TValue value = eachToken[1].ToObject<TValue>(serializer);
dictionary.Add(key, value);
}
return dictionary;
}
}
}