4 people like it.

Datatype generic JSON string <-> type

A simple example of using datatype generic JSON operations from Infers.Toys.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: 
58: 
59: 
60: 
61: 
62: 
63: 
64: 
65: 
66: 
67: 
68: 
69: 
70: 
71: 
72: 
73: 
74: 
75: 
76: 
77: 
78: 
79: 
80: 
81: 
82: 
83: 
// How do you parse some fields from JSON like this (from Google geocoding API)?

let replyString = """{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "2",
               "short_name" : "2",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Mannerheimintie",
               "short_name" : "Mannerheimintie",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Helsinki",
               "short_name" : "HKI",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Helsinki",
               "short_name" : "Helsinki",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Finland",
               "short_name" : "FI",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "00100",
               "short_name" : "00100",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Mannerheimintie 2, 00100 Helsinki, Finland",
         "geometry" : {
            "location" : {
               "lat" : 60.1667917,
               "lng" : 24.9426541
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 60.16814068029151,
                  "lng" : 24.9440030802915
               },
               "southwest" : {
                  "lat" : 60.1654427197085,
                  "lng" : 24.9413051197085
               }
            }
         },
         "place_id" : "ChIJhfLf_MsLkkYRuZa8FC_DScU",
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
}
"""

// First you define types for the things you are interested in:

type AddressComponent =
  {long_name: string; short_name: string; types: array<string>}
type Coord = {lat: float; lng: float}
type Geometry = {location: Coord}
type Result =
  {address_components: array<AddressComponent>
   geometry: Geometry}
type Reply = {status: string; results: array<Result>}

// Then you can use Infers.Toys to convert from JSON to the defined type:

open Infers.Toys

let reply = ofJsonString<Reply> replyString

// And also convert from the type back to JSON:

let replyPartString = toJsonString reply
val replyString : string

Full name: Script.replyString
type AddressComponent =
  {long_name: string;
   short_name: string;
   types: string array;}

Full name: Script.AddressComponent
AddressComponent.long_name: string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
AddressComponent.short_name: string
AddressComponent.types: string array
type 'T array = 'T []

Full name: Microsoft.FSharp.Core.array<_>
type Coord =
  {lat: float;
   lng: float;}

Full name: Script.Coord
Coord.lat: float
Multiple items
val float : value:'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float

--------------------
type float = System.Double

Full name: Microsoft.FSharp.Core.float

--------------------
type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>
Coord.lng: float
type Geometry =
  {location: Coord;}

Full name: Script.Geometry
Geometry.location: Coord
type Result =
  {address_components: AddressComponent array;
   geometry: Geometry;}

Full name: Script.Result
Result.address_components: AddressComponent array
Result.geometry: Geometry
type Reply =
  {status: string;
   results: Result array;}

Full name: Script.Reply
Reply.status: string
Reply.results: Result array
namespace Infers
namespace Infers.Toys
val reply : Reply

Full name: Script.reply
val ofJsonString : string -> 't

Full name: Infers.Toys.Json.ofJsonString
val replyPartString : string

Full name: Script.replyPartString
val toJsonString : 't -> string

Full name: Infers.Toys.Json.toJsonString
Raw view Test code New version

More information

Link:http://fssnip.net/7PM
Posted:7 years ago
Author:Vesa Karvonen
Tags: generic functions , json