public TEnum Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) { if (reader.ReadIsNull()) { return(null); } return(SmartFlagEnum <TEnum, TValue> .DeserializeValue(ReadValue(ref reader))); }
public TEnum Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) { if (reader.ReadIsNull()) { return(null); } var name = reader.ReadString(); return(SmartFlagEnum <TEnum, TValue> .FromName(name).FirstOrDefault()); }
private TEnum GetFromValue(TValue value) { try { return(SmartFlagEnum <TEnum, TValue> .DeserializeValue(value)); } catch (Exception ex) { throw new JsonException($"Error converting value '{value}' to a smart flag enum.", ex); } }
public TEnum Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) { if (MessagePackBinary.IsNil(bytes, offset)) { readSize = 1; return(null); } var name = MessagePackBinary.ReadString(bytes, offset, out readSize); return(SmartFlagEnum <TEnum, TValue> .FromName(name).FirstOrDefault()); }
/// <summary> /// /// </summary> /// <param name="reader"></param> /// <param name="objectType"></param> /// <param name="existingValue"></param> /// <param name="hasExistingValue"></param> /// <param name="serializer"></param> /// <returns></returns> public override TEnum ReadJson(JsonReader reader, Type objectType, TEnum existingValue, bool hasExistingValue, JsonSerializer serializer) { try { TValue value; if (reader.TokenType == JsonToken.Integer && typeof(TValue) != typeof(long) && typeof(TValue) != typeof(bool)) { value = (TValue)Convert.ChangeType(reader.Value, typeof(TValue)); } else { value = (TValue)reader.Value; } return(SmartFlagEnum <TEnum, TValue> .DeserializeValue(value)); } catch (Exception ex) { throw new JsonSerializationException($"Error converting {reader.Value ?? "Null"} to {objectType.Name}.", ex); } }
public override TEnum ReadJson(JsonReader reader, Type objectType, TEnum existingValue, bool hasExistingValue, JsonSerializer serializer) { switch (reader.TokenType) { case JsonToken.String: return(GetFromName((string)reader.Value)); default: throw new JsonSerializationException($"Unexpected token {reader.TokenType} when parsing a smart flag enum."); } TEnum GetFromName(string name) { try { return(SmartFlagEnum <TEnum, TValue> .FromName(name, false).FirstOrDefault()); } catch (Exception ex) { throw new JsonSerializationException($"Error converting value '{name}' to a smart flag enum.", ex); } } }
public override TEnum Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { switch (reader.TokenType) { case JsonTokenType.String: return(GetFromName(reader.GetString())); default: throw new JsonException($"Unexpected token {reader.TokenType} when parsing a smart flag enum."); } TEnum GetFromName(string name) { try { return(SmartFlagEnum <TEnum, TValue> .FromName(name, false).FirstOrDefault()); } catch (Exception ex) { throw new JsonException($"Error converting value '{name}' to a smart flag enum.", ex); } } }