Skip to content

Instantly share code, notes, and snippets.

@karronoli
Last active December 23, 2018 02:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karronoli/3b451c276f60851eef7ae48ec2d8b8ce to your computer and use it in GitHub Desktop.
Save karronoli/3b451c276f60851eef7ae48ec2d8b8ce to your computer and use it in GitHub Desktop.
dotnet new console -lang F# -o sample && dotnet add package Manatee.Json && dotnet run # source: https://qiita.com/karronoli/items/46f6892999ee253d6396
{
"name": "ペン",
"count": 3
}
{
"name": "\u30da\u30f3",
"count": 3
}
// for Manatee.Json ver10
open System
open System.IO
open System.Linq
open Manatee.Json
open Manatee.Json.Schema
open Manatee.Json.Serialization
type Item() =
member val Name = "" with get, set
member val Count = 0 with get, set
[<EntryPoint>]
let main argv =
let serializer = JsonSerializer()
let schema =
File.ReadAllText (if argv.Length > 0 then argv.[0] else "schema.json")
|> JsonValue.Parse
|> serializer.Deserialize<JsonSchema>
let json =
File.ReadAllText (if argv.Length > 1 then argv.[1] else "item.json")
|> JsonValue.Parse
let result = schema.Validate json
if not (result.IsValid)
then
result.AdditionalInfo.Select (fun i -> i.Key + i.Value.ToString())
|> String.concat "\n"
|> System.Exception
|> raise
printfn "Schema OK"
let item = serializer.Deserialize<Item> json
printfn "Name: %s, Count: %d" item.Name item.Count
0
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Program.fs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Manatee.Json" Version="10.1.1" />
</ItemGroup>
</Project>
{
"$schema": "http:\/\/json-schema.org\/draft-07\/schema#",
"type": "object",
"properties": {
"name": {
"type": "string"
},
"count": {
"type": "integer"
}
},
"required": [
"name",
"count"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment