public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { // make sure it has a value; optional instances without a value must be ignored IOptional optional = (IOptional)value; if (!optional.HasValue) { string optionalValueTypeName = optional.GetType().GenericTypeArguments.Single().Name; throw new InvalidOperationException(("Optional<{0}>.HasValue is false. " + "Optional properties should include these attributes: " + "[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Include), DefaultValueDefault(typeof(Optional<{0}>))]") .FormatInvariant(optionalValueTypeName)); } // serialize value serializer.Serialize(writer, optional.Value); }
private void ReadOption(ref Utf8JsonReader reader, IOptional target, JsonSerializerOptions options) { reader.Read(); // read property name if (reader.GetString() == "HasValue") { reader.Read(); // read property value target.HasValue = reader.GetBoolean(); } if (!target.HasValue) { reader.Read(); // property name "Value" reader.Read(); // property "Value" null value reader.Read(); // end of Optional<T> object return; } PropertyInfo propertyInfo = null; while (reader.Read()) { if (reader.TokenType == JsonTokenType.StartObject) { target.Value = ReadObject(ref reader, options); } else if (reader.TokenType == JsonTokenType.EndObject) { return; } else if (reader.TokenType == JsonTokenType.PropertyName) { string propertyName = reader.GetString(); // must be "Value" propertyInfo = target.GetType().GetProperty(propertyName); } else if (reader.TokenType == JsonTokenType.Null) { target.Value = null; } else if (reader.TokenType == JsonTokenType.True) { target.Value = true; } else if (reader.TokenType == JsonTokenType.False) { target.Value = false; } else if (reader.TokenType == JsonTokenType.Number) { uint intValue = reader.GetUInt32(); if (propertyInfo.PropertyType.IsEnum) { target.Value = Enum.GetValues(propertyInfo.PropertyType).GetValue(intValue); } else { target.Value = intValue; } } else if (reader.TokenType == JsonTokenType.String) { string stringValue = reader.GetString(); if (propertyInfo.PropertyType == typeof(Type)) { target.Value = Type.GetType(stringValue); } else if (propertyInfo.PropertyType == typeof(Assembly)) { //AppDomain.CurrentDomain.GetAssemblies().SingleOrDefault(assembly => assembly.GetName().Name == name); target.Value = Assembly.Load(stringValue); } else { target.Value = stringValue; } } else if (reader.TokenType == JsonTokenType.StartArray) { IList list = (IList)propertyInfo.GetValue(target); if (list == null) { list = (IList)propertyInfo.PropertyType.GetConstructor(Type.EmptyTypes).Invoke(null); propertyInfo.SetValue(target, list); } while (reader.TokenType != JsonTokenType.EndArray) { if (!reader.Read() || reader.TokenType == JsonTokenType.EndArray) { break; } DummyAssembly item = ReadObject(ref reader, options); list.Add(item); } } } }
public static InvalidOperationException IsEmptyException <T>([NotNull] IOptional <T> self) { return(new InvalidOperationException($"Unable to retrieve the {nameof(self.Value)} from the {self.GetType().PrettifyType(default)} because it is empty!"));