/// <summary> /// Constructor for given json content and optional JsonSerializer Settings. /// </summary> /// <param name="patchDocument">json MergePatchDocument content.</param> /// <param name="settings">Serializer settings to use. NullValueHandling will always be NullValueHandling.Include though.</param> /// <exception cref="ArgumentException">if <paramref name="patchDocument" /> is null or whitespace.</exception> /// <exception cref="InvalidJsonMergePatchDocumentException"> /// if <paramref name="patchDocument" /> anything other than a /// parsable json object. /// </exception> public JsonMergePatchDocument(string patchDocument, JsonSerializerSettings settings = null) { if (string.IsNullOrWhiteSpace(patchDocument)) { throw new ArgumentException("Value cannot be null or whitespace.", nameof(patchDocument)); } _serializerSettings = settings ?? new JsonSerializerSettings(); var serializer = JsonSerializer.Create(_serializerSettings); _internalValidator = new InternalValidator <TResource>(); // Ensure that the serializer does not ignore null values to comply with RFC 7386 serializer.NullValueHandling = NullValueHandling.Include; if (!patchDocument.Trim().StartsWith("{")) { throw new InvalidJsonMergePatchDocumentException(ErrorMessages.DocumentRootMustBeObject); } try { var patchObject = JObject.Parse(patchDocument); _internalDocument = new PatchDocument(patchObject, typeof(TResource), serializer); } catch (JsonReaderException ex) { throw new InvalidJsonMergePatchDocumentException(ErrorMessages.DocumentNotParseable, ex); } }
/// <summary> /// Constructor for given json content and optional JsonSerializer Settings. /// </summary> /// <param name="reader">JsonReader.</param> /// <param name="serializer">settings</param> /// <exception cref="InvalidJsonMergePatchDocumentException"> /// if <paramref name="reader" /> points to anything other than a /// parsable json object. /// </exception> public JsonMergePatchDocument(JsonReader reader, JsonSerializer serializer) { _internalValidator = new InternalValidator <TResource>(); if (reader.TokenType != JsonToken.StartObject) { throw new InvalidJsonMergePatchDocumentException(ErrorMessages.DocumentRootMustBeObject); } try { var patchObject = JObject.Load(reader); _internalDocument = new PatchDocument(patchObject, typeof(TResource), serializer); } catch (JsonReaderException ex) { throw new InvalidJsonMergePatchDocumentException(ErrorMessages.DocumentNotParseable, ex); } }