Last active
December 23, 2018 02:36
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "ペン", | |
"count": 3 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "\u30da\u30f3", | |
"count": 3 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"$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