public static DefaultTypeConverter Create() { var registry = new RegistryTypeConverter(); var result = new DefaultTypeConverter(registry, new IdentityTypeConverter(), new ElementToArrayTypeConverter(), new SystemConvertTypeConverter()); registry.Register <object, JToken>(LambdaTypeConverter.Create(x => x == null ? null : JToken.FromObject(x))); registry.Register <JToken, object>(LambdaTypeConverter.Create((x, type) => ((JToken)x).ToObject(type))); registry.Register <string, HttpBody>(LambdaTypeConverter.Create(x => new StringHttpBody((string)x))); registry.Register <byte[], HttpBody>(LambdaTypeConverter.Create(x => new ByteArrayHttpBody((byte[])x))); registry.Register <Stream, HttpBody>(LambdaTypeConverter.Create(x => new StreamHttpBody((Stream)x))); registry.Register <Array, Array>(new ArrayTypeConverter()); registry.Register <Enum, string>(LambdaTypeConverter.Create(x => EnumMemberCache.GetEnumMemberName((Enum)x))); registry.Register <string, Enum>(LambdaTypeConverter.Create((x, type) => EnumMemberCache.GetEnumMemberByName(type, (string)x))); registry.Register <bool, string>(LambdaTypeConverter.Create(x => (bool)x ? "true" : "false")); registry.Register <string, bool>(LambdaTypeConverter.Create(x => bool.Parse((string)x))); return(result); }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { string str = EnumMemberCache.GetString(value); writer.WriteValue(str); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { object val = EnumMemberCache.GetValue(reader.Value as string, objectType); return(val); }
public static string GetEnumMemberValue <T>(this T value) where T : struct, Enum => EnumMemberCache <T> .Get(value);