public JsonObject(string key, IJsonElement element) { Content = new Dictionary <string, IJsonElement>(StringComparer.OrdinalIgnoreCase) { [key] = element }; }
public static object Patch(this IPatcher patcher, object patchedObject, IJsonElement json) { if (patchedObject == null) { throw new ArgumentNullException("patchedObject"); } return(patcher.PatchValue(patchedObject, patchedObject.GetType(), json)); }
public virtual IPatcher GetPatcher(Type expectedType, IJsonElement json) { if (typeof(IEntityBase).IsAssignableFrom(expectedType)) { throw new ForbiddenException("It is not permitted to patch a property of type derived from IEntityBase"); } if (expectedType.IsClass) { if (Services.GetService(typeof(IPatcher <>).MakeGenericType(new[] { expectedType })) is IPatcher foundPatcher && foundPatcher.GetType() != typeof(ObjectPatcher <>).MakeGenericType(new[] { expectedType })) { return(foundPatcher); } } if (json is JsonArray) { return(Services.GetRequiredService <EnumerablePatcher>()); } if (expectedType.IsSubclassOfInterface(typeof(IDictionary))) { return(Services.GetRequiredService <DictionaryPatcher>()); } if (ReflectionHelper.IsPseudoValue(expectedType) || expectedType.GetNullableType().IsValueType) { return(Services.GetRequiredService <ValuePatcher>()); } if (expectedType == typeof(object)) { if (json is JsonValue) { return(Services.GetRequiredService <ValuePatcher>()); } if (json is JsonObject) { return(Services.GetRequiredService <DynamicPatcher>()); } } return(Services.GetRequiredService <ObjectPatcher>()); }
public SimpleJsonParser( string fullJsonString ) { string jsonRemaining; Parsed = ParseOne( fullJsonString, out jsonRemaining ); // For valid json, top level must be a json object if ( (Parsed != null) && Parsed.IsObject() ) { Success = true; } else { // If conditions not met, invalid Success = false; } }
object IPatcher.PatchValue(object patchedObject, Type expectedType, IJsonElement json) { return(PatchValue(patchedObject, expectedType, json as JsonArray)); }
public object PatchValue(object patchedObject, Type expectedType, IJsonElement json) => throw new NotImplementedException();
protected virtual void PatchProperty(object patchedObject, PropertyInfo property, IJsonElement element) { var patcher = Provider.GetPatcher(property.PropertyType, element); var initialValue = patcher.InitialValue(property, patchedObject); var value = patcher.PatchValue(initialValue, property.PropertyType, element); PatchProperty(patchedObject, value, property); }
protected virtual void PatchKey(Dictionary <string, PropertyInfo> properties, object patchedObject, Type entityType, string key, IJsonElement element) { if (!properties.ContainsKey(key)) { throw new BadRequestException($"Property {key} does not exist on type {entityType.Name}"); } if (!properties[key].CanWrite) { throw new BadRequestException($"Property {key} cannot be written to."); } PatchProperty(patchedObject, properties[key], element); }
protected override void PatchKey(Dictionary <string, PropertyInfo> properties, object patchedObject, Type entityType, string key, IJsonElement element) { if (string.Equals(key, _configuration.Discriminator, StringComparison.OrdinalIgnoreCase)) { return; } base.PatchKey(properties, patchedObject, entityType, key, element); }