forked from atenfyr/UAssetAPI
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ByteArrayJsonConverter, MetadataPropertyHandling.ReadAhead
- Loading branch information
Showing
2 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace UAssetAPI.JSON | ||
{ | ||
public class ByteArrayJsonConverter : JsonConverter | ||
{ | ||
public override bool CanConvert(Type objectType) | ||
{ | ||
return objectType == typeof(byte[]); | ||
} | ||
|
||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
writer.WriteValue(Convert.ToBase64String((byte[])value)); | ||
} | ||
|
||
public override bool CanRead | ||
{ | ||
get { return true; } | ||
} | ||
|
||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
string val = reader.Value as string; | ||
|
||
// backwards compatibility for fun | ||
if (reader.TokenType == JsonToken.StartObject) | ||
{ | ||
reader.Read(); | ||
while (reader.TokenType != JsonToken.EndObject) | ||
{ | ||
if (reader.TokenType != JsonToken.PropertyName) throw new FormatException("Expected PropertyName, got " + reader.TokenType); | ||
string propertyName = reader.Value.ToString(); | ||
reader.Read(); | ||
object propertyValue = reader.Value; | ||
reader.Read(); | ||
|
||
if (propertyName == "$value") | ||
{ | ||
val = propertyValue as string; | ||
} | ||
} | ||
} | ||
|
||
if (val == null) return null; | ||
return Convert.FromBase64String(val); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters