Code Monkey home page Code Monkey logo

hal-json-net's Introduction

hal-json-net

HAL JSON support for Json.Net

Model:

public class Model
{
	public uint Id { get; set; }
	public string Name { get; set; }
	public List<int> ItemIds { get; set; }
}

Configuration:

var config = HalJsonConfiguration("http://example.com/api"); //You can use null instead of example.com, it won't touch your links in that case
config.Configure<Model>()
				.Embed(p => p.ItemIds)
				.Link("self", x => "/model/" + x.Id)
				.Link("all", "/model");
				
var serializer = new JsonSerializer{ContractResolver = new JsonNetHalJsonContactResolver(config)};
//Use serializer here

Results:

{
  "_links": {
    "all": {
      "href": "http://example.com/api/model",
      "templated": "false"
    },
    "self": {
      "href": "http://example.com/api/model/42",
      "templated": "false"
    }
  },
  "_embedded": {
    "itemIds": [
      1,
      2,
      3
    ]
  },
  "id": 42,
  "name": "One"
}

You may as well use IHaveHalJsonLinks and IHaveHalJsonEmbedded:

public class AdvancedModel: IHaveHalJsonLinks, IHaveHalJsonEmbedded
{
    public IDictionary<string, Link> GetLinks()
    {
        return new Dictionary<string, Link> {{"self", "/something"}};
	}

    public IDictionary<string, Embedded> GetEmbedded()
    {
	    return new Dictionary<string, Embedded> {{"ids", new Embedded(new[] {1, 2, 3})}};
    }
}

Or attributes:

[HalJsonLink("something", "123")]
public class AttributeBasedModel
{
    [HalJsonLink("self", "/mdl/{0}")]
    public int Id { get; set; }

    [HalJsonEmbedded("ids")]
    public List<int> Ids { get; set; }
}

Or even mix them together.

Differencies from other JSON.NET libraries

https://github.com/MLaritz/HalJsonConverter
  • You don't have to hardcode HAL information inside the model (it's impossible to configure base url in MLaritz's implementation)
  • My implementation doesn't duplicate Json.Net serialization infrastructure, just changes it's configuration, you won't loss your serialization settings configured by Json.Net built-in attribute system (i.e. JsonProperty, JsonIgnore, before/after serialization callbacks, etc)

hal-json-net's People

Contributors

kekekeks avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  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.