Home

Awesome

BinaryDefense.JsonWrapper

What

A plugin for Myriad for generating statically typed lossless wrappers around JToken given a schema.

Why

When serializing JSON directly into known record types, this can cause the underlying dataset to be lost. Given a JSON object:

{
    "Name" : "James Kirk",
    "Age"  : 32
}

and a record type:


type Person = {
    Name : string
    Age : uint32
}

serializing is fine. However, if the underlying json adds more information in it's payload such as:

{
    "Name" : "James Kirk",
    "Age"  : 32,
    "UniformColor": "Gold"
}

If that type is then serialized and saved somewhere, our record type will lose the UniformColor field, causing this to be a lossy conversion.

This Myriad plugin will instead generate a class given a record type as a schema.

How

Add BinaryDefense.Myriad.Plugins.JsonWrapper to your project.

Given our record above as a schema, we need to add the attribute [<Generator.JsonWrapper>] to it.

[<Generator.JsonWrapper>]
type Person = {
    Name : string
    Age : uint32
}

Additionally, an entry to the fsproj file must be added:

<Compile Include="MyGeneratedFile.fs">
    <MyriadFile>MyTypes.fs</MyriadFile>
    <MyriadNameSpace>GeneratedNamespace</MyriadNameSpace>
</Compile>

MyTypes.fs is where the Person record type lives. MyGeneratedFile.fs will be the file generated with the output. GeneratedNamespace will be the namespace the generated code lives in. On build, Myriad will generate the code An example of it's output:

type Person(jtoken: JToken, serializer: JsonSerializer) =
    member this.Name
        with get () =
            let selectedToken = jtoken.["Name"]
            selectedToken.ToObject<string> serializer
        and set (newValue: string) =
            jtoken.["Name"] <- JToken.FromObject(newValue, serializer)

    member this.Age
        with get () =
            let selectedToken = jtoken.["Age"]
            selectedToken.ToObject<uint32> serializer
        and set (newValue: uint32) =
            jtoken.["Age"] <- JToken.FromObject(newValue, serializer)

    override this.GetHashCode () = jtoken.GetHashCode()

    override this.Equals(objToCompare: obj) =
        match objToCompare with
        | :? IHaveJToken as jTokenToCompare -> JToken.DeepEquals(jTokenToCompare.InnerData, jtoken)
        | _ -> false

    ///This allows the class to be pattern matched against
    member this.Deconstruct(Name: outref<int>, Age: outref<string>) =
        Name <- this.Name
        Age <- this.Age

    interface IHaveJToken with
        override this.InnerData = jtoken

When using this type, you'll need to also add custom converters to the JsonSerializer you are using throughout your application.

let converters = Converters.recommendedConverters

let serializationSettings =
    let s = JsonSerializerSettings()
    scrubDefaultDUConverter s.Converters
    for c in converters do s.Converters.Add c
    s

let jsonSettings = serializationSettings
let jsonSerializer =JsonSerializer.CreateDefault jsonSettings

This will be using the IHaveJTokenConverter to ensure the serialization is lossless.

Additional reading on this topic:

Features


Builds

GitHub Actions
GitHub Actions
Build History

NuGet

PackageStablePrerelease
BinaryDefense.JsonWrapper.CoreNuGet BadgeNuGet Badge
BinaryDefense.Myriad.Plugins.JsonWrapperNuGet BadgeNuGet Badge

Developing

Make sure the following requirements are installed on your system:

or


Environment Variables


Building

> build.cmd <optional buildtarget> // on windows
$ ./build.sh  <optional buildtarget>// on unix

The bin of your library should look similar to:

$ tree src/MyCoolNewLib/bin/
src/MyCoolNewLib/bin/
└── Debug
    ├── net461
    │   ├── FSharp.Core.dll
    │   ├── MyCoolNewLib.dll
    │   ├── MyCoolNewLib.pdb
    │   ├── MyCoolNewLib.xml
    └── netstandard2.1
        ├── MyCoolNewLib.deps.json
        ├── MyCoolNewLib.dll
        ├── MyCoolNewLib.pdb
        └── MyCoolNewLib.xml


Build Targets


Releasing

git add .
git commit -m "Scaffold"
git remote add origin https://github.com/user/MyCoolNewLib.git
git push -u origin master

NOTE: Its highly recommend to add a link to the Pull Request next to the release note that it affects. The reason for this is when the RELEASE target is run, it will add these new notes into the body of git commit. GitHub will notice the links and will update the Pull Request with what commit referenced it saying "added a commit that referenced this pull request". Since the build script automates the commit message, it will say "Bump Version to x.y.z". The benefit of this is when users goto a Pull Request, it will be clear when and which version those code changes released. Also when reading the CHANGELOG, if someone is curious about how or why those changes were made, they can easily discover the work and discussions.

Here's an example of adding an "Unreleased" section to a CHANGELOG.md with a 0.1.0 section already released.

## [Unreleased]

### Added
- Does cool stuff!

### Fixed
- Fixes that silly oversight

## [0.1.0] - 2017-03-17
First release

### Added
- This release already has lots of features

[Unreleased]: https://github.com/user/MyCoolNewLib.git/compare/v0.1.0...HEAD
[0.1.0]: https://github.com/user/MyCoolNewLib.git/releases/tag/v0.1.0

macOS/Linux Parameter:

./build.sh Release 0.2.0

macOS/Linux Environment Variable:

RELEASE_VERSION=0.2.0 ./build.sh Release