Git Product home page Git Product logo

jsonref.net's Introduction

JsonRef.NET

Library for resolving JSON-Ref and JSON-LD style object references. Based on System.Text.Json.

#r "nuget: JsonRef.NET"

open JsonRef.NET

JsonRefResolver.resolve jsonString

Background

JSON-LD supports references to objects with a reserved key @id which can be used either as an object identifier or a reference to an object identifier. The value of @id must be a string. E.g. The following object:

{
      "@id" : "TheID",
      "value" : "MyValue"
}

might be referenced in another part of the json file as follows:

{
      "@id" : "TheID"
}

Usage

This kind of reference can be resolved with a single function call, either with a serialized json string as input or on a deserialized System.Text.Json JsonNode.

E.g. the following json object based on the example above:

let simpleJson = 
"""
{
    "source": {
        "@id" : "TheID",
        "value" : "MyValue"
    },
    "target": {
        "@id" : "TheID"
    }
}
"""

can be resolved as follows:

#r "nuget: JsonRef.NET"

open JsonRef.NET

JsonRefResolver.resolve simpleJson

resulting in:

{
    "source": {
      "@id" : "TheID",
      "value" : "MyValue"
    },
    "target": {
      "@id" : "TheID",
      "value" : "MyValue"
    }
}

or alternatively working on a System.Text.Json JsonNode:

#r "nuget: JsonRef.NET"

open JsonRef.NET
open System.Text.Json

let jsonNode = Nodes.JsonObject.Parse(simpleJson)

JsonRefResolver.resolve jsonNode

Recursive references

Currently there is no support for autodetection of recursive references. Instead, you can give a exclusion lists for fields that should not be resolved:

#r "nuget: JsonRef.NET"

open JsonRef.NET

let options = JsonRefResolverOptions(IgnoreFields = [|"ignoreValue"|])

JsonRefResolver.resolve(jsonString,options)

Resolve references across files

If you want to insert JSON-LD objects of one json string into referencing objects of another string, you can use the following approach:

#r "nuget: JsonRef.NET"

open JsonRef.NET
open System.Text.Json

let referencejsonNode = Nodes.JsonObject.Parse(referenceJsonString)
let targetjsonNode = Nodes.JsonObject.Parse(targetJsonString)
let options = JsonRefResolverOptions.defaultOptions

let referenceObjects = JsonRefResolver.collectObjects(referencejsonNode,options)

JsonRefResolver.fillObjects(referenceObjects,targetjsonNode,options)
|> fun s -> s.ToJsonString()

jsonref.net's People

Contributors

hlweil avatar

Stargazers

 avatar

Watchers

 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.