Git Product home page Git Product logo

partial.newtonsoft.json's Introduction

Partial.Newtonsoft.Json

Build Status GitHub Tag NuGet Count Issues Open

A small helper library to allow deserialization (and serialization) of JSON fragments. Helpful for PATCH or partial PUT operations in web API projects.

Installation

Install the library via NuGet. All you need to do is manage your NuGet packages via Visual Studio or run

nuget install Partial.Newtonsoft.Json

in the package manager command line. Alternatively, you can install the library via the command line using the dotnet tooling.

Usage

⚠️⚠️ The namespace of this project is Newtonsoft.Json.Partial. ⚠️⚠️

The main class is called Part<T>. This class is a wrapper that should be used for deserializing objects with information about the used keys.

Simple example

// The DTO
public class Employee
{
    [JsonProperty(PropertyName = "id")]
    public string Id { get; set; }

    [JsonProperty(PropertyName = "name")]
    public string Name { get; set; }

    [JsonProperty(PropertyName = "age")]
    public int Age { get; set; }
}

// The incoming JSON
var json = "{\"id\": 17, \"name\": \"Test\"}";

// Usually, we do this to obtain the deserialized object
var normalObj = JsonConvert.DeserializeObject<Employee>(json);

// Now we can also do this to obtain the deserialized object and more information
var partialObj = JsonConvert.DeserializeObject<Part<Employee>>(json);

Note that partialObj.Data is equal to normalObj, however, in partialObj we also have a Keys property.

Using the information

Once we have a Part<T> object we can use it to get information to the used Keys or use one of the extension methods.

var partialObj = JsonConvert.DeserializeObject<Part<Employee>>(json);

if (partialObj.IsSet(m => m.Age))
{
    // Do something if Age has been set
}

Alternatively, we can also supply a callback action.

var partialObj = JsonConvert.DeserializeObject<Part<Employee>>(json);

partialObj.IsSet(m => m.Age, value =>
{
    // Do something with the supplied value of Age
});

It is also possible to perform some validation on the supplied JSON object to verify that no "illegal" properties have been specified:

var partialObj = JsonConvert.DeserializeObject<Part<Employee>>(json);

// Perform validation of the partial object
var isValid = partialObj.Validate();

Decorating Data Transfer Objects

In addition to the usual JsonProperty, JsonIgnore, etc. attributes there is also the JsonNoPartial attribute. It specifies that in context of partial deserialization that property must not be set.

License

MIT License (MIT). For more information see LICENSE file.

partial.newtonsoft.json's People

Contributors

florianrappl avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.