Skip to content

Latest commit

 

History

History
441 lines (329 loc) · 6.42 KB

Json-Ref.md

File metadata and controls

441 lines (329 loc) · 6.42 KB

Server-side JSON Cheat Sheet

Here is a quick reference for the JSON format used by [<Json>]-annotated inferred sitelets, Content.JsonContent and WebSharper.Json.

Base types

Encoded as JSON primitive types.

TypeValue and corresponding encoded JSON
type Body = string
"home"

// "home"

"This is \"home\"!"

// "This is \"home\"!"
type Body = int
2

// 2
type Body = float
1.2345

// 1.2345
And every standard .NET number type: int8, uint8, int16, uint16, int32, uint32, int64, uint64, single, double, decimal. ## Records

Encoded as a JSON object.

TypeValue and corresponding encoded JSON
type Action =
  { x: string
    y: int }
{ x = "test"
  y = 1 }

// {"x":"test","y":1}
type Action =
  { x: string
    y: Sub }
and Sub =
  { z: int
    t: int }
{ x = "test"
  y =
    { z = 1
      t = 2 } }

// {"x":"test","y":{"z":1,"t":2}}
## Unions

Encoded as a JSON object. Use [<NamedUnionCases(fieldname)>] to store the case name in a field, and [<Name(name)>] for the name of each field.

TypeValue and corresponding encoded JSON
[<NamedUnionCases "result">]
type Body =
  | [<Name "success">]
    Success of value: int
  | [<Name "error">]
    Error of message: string
Success 42

// {"result":"success","value":42}

Error "Wrong value."

// {"result":"error","message":"Wrong value."}

Use [<NamedUnionCases>] to infer the union case based on the present field names.

[<NamedUnionCases>]
type Body =
  | Success of value: int
  | Error of error: string
Success 42

// {"value":42}

Error "Incorrect value."

// {"error":"Incorrect value."}

A single unnamed record argument is treated as if its fields were the arguments.

[<NamedUnionCases "result">]
type Body =
  | [<Name "success">]
    Success of Value
  | [<Name "error">]
    Error of message: string

and Value =
  { id: int
    name: string }
Success { id = 1; name = "abc" }

// {"result":"success","id":1,"name":"abc"}

Use [<Constant>] to represent an argument-less union case as a string, int, float or bool instead of an object.

type Body =
  | [<Constant "red">] Red
  | [<Constant true>] Green
  | [<Constant 3>] Blue
[Red; Green; Blue]

// ["red",true,3]
## Options

Union case arguments of type option become present/absent fields.

TypeValue and corresponding encoded JSON
[<NamedUnionCases>]
type Body =
  | Success of value: int
             * remark: string option
  | Error of error: string
Success(41, Some "Almost there...")

// {"value":41,"remark":"Almost there..."}

Success(42, None)

// {"value":42}

Record fields of type option become present/absent fields.

type Body =
  { value: int
    remark: string option }
{ value = 41; remark = Some "Almost there..." }

// {"value":41,"remark":"Almost there..."}

{ value = 42; remark = None }

// {"value":42}
## Collections

Arrays, lists and sets are represented as JSON arrays.

TypeValue and corresponding encoded JSON
type Body = int[]
[| 4; 8; 15; 16; 23; 42 |]

// [4,8,15,16,23,42]
type Body = Person list

and Person =
  { name: string }
[
  { name = "John" }
  { name = "Bob" }
]

// [{"name":"John"},{"name":"Bob"}]
type Body = Set<string>
Set [ "fsharp"; "websharper" ]

// ["fsharp","websharper"]

Maps and dictionaries with string keys are represented as JSON objects.

type Body = Map<string, int>
Map [
  "John", 38
  "Bob", 46
]

// {"John":38,"Bob":46}
open System.Collections.Generic

type Body = Dictionary<string, Person>

and Person =
  { age: int }
let dict = Dictionary()
dict.["John"] <- { age = 38 }
dict.["Bob"] <- { age = 46 }

// {"John":{"age":38},"Bob":{"age":46}}
## Datetime

Encoded as a string. Default format is ISO-8601 round-trip format ("o").

TypeValue and corresponding encoded JSON
type Action = System.DateTime
System.DateTime.UtcNow

// "2015-04-15T15:37:23.0000000"

Setting the format of a DateTime union case argument with [<DateTimeFormat(argname, format)>].

type Body =
  | [<DateTimeFormat("date", "yyyy-MM-dd")>]
    Article of id: int * date: DateTime
Article(43, System.DateTime.Now)

// {"id":43,"date":"2015-04-15"}

Setting the format of a DateTime record field with [<DateTimeFormat(format)>].

type Body =
  { id: int
    [<DateTimeFormat("yyyy-MM-dd")>]
    date: DateTime }
{ id = 43; date = System.DateTime.Now }

// {"id":43,"date":"2015-04-15"}

Both DateTimeFormat uses also work on DateTime option.

[<NamedUnionCases>]
type Body =
  | [<DateTimeFormat("time", "HH:mm:ss")>]
    Body of time: DateTime option * child: Child

and Child =
  { [<DateTimeFormat("yyyy-MM-dd")>]
    date: DateTime option }
Body(Some System.DateTime.Now, { date = None })

// {"time":"15:37:23","child":{}}

Body(None, { date = Some System.DateTime.Now })

// {"child":{"date":"2015-04-15"}}