12

r@".NETFramework\v4.0\Profile\Client\System.Runtime.Serialization.dll"

open System.Runtime.Serialization
open System.Runtime.Serialization.Json

[<DataContract>]
    type geo = {
        [<field: DataMember(Name = "type")>]
        t:string
        [<field: DataMember(Name = "coordinates")>]
        coordinates:string
        }


let decode (s:string)  = 
    let json = new DataContractJsonSerializer(typeof<geo>)
    let byteArray = Encoding.UTF8.GetBytes(s)
    let stream = new MemoryStream(byteArray)
    json.ReadObject(stream) :?> geo

let tw = {"type":"Point","coordinates":[-7.002648,110.449961]}

decode tw 

This returns -> End element 'coordinates' from namespace '' expected. Found element 'item' from namespace ''

How can I define the DataMember coordinates so that it understands ?

Thanks a lot

Community
  • 1
  • 1
jlezard
  • 1,417
  • 2
  • 15
  • 32

2 Answers2

8

Reference System.Runtime.Serialization and System.Xml

(Interactive: #r "System.Runtime.Serialization" )

open System.IO
open System.Runtime.Serialization.Json
open System.Xml
open System.Text

/// Object to Json 
let internal json<'t> (myObj:'t) =   
        use ms = new MemoryStream() 
        (new DataContractJsonSerializer(typeof<'t>)).WriteObject(ms, myObj) 
        Encoding.Default.GetString(ms.ToArray()) 


/// Object from Json 
let internal unjson<'t> (jsonString:string)  : 't =  
        use ms = new MemoryStream(ASCIIEncoding.Default.GetBytes(jsonString)) 
        let obj = (new DataContractJsonSerializer(typeof<'t>)).ReadObject(ms) 
        obj :?> 't
Tuomas Hietanen
  • 4,650
  • 2
  • 35
  • 43
6

this works for me

#r "System.Runtime.Serialization"

open System.IO
open System.Text
open System.Runtime.Serialization
open System.Runtime.Serialization.Json

[<DataContract>]
    type geo = {
        [<field: DataMember(Name = "type")>]
        t:string
        [<field: DataMember(Name = "coordinates")>]
        coordinates:float[]
        }


let decode (s:string)  = 
    let json = new DataContractJsonSerializer(typeof<geo>)
    let byteArray = Encoding.UTF8.GetBytes(s)
    let stream = new MemoryStream(byteArray)
    json.ReadObject(stream) :?> geo

let tw = "{
    \"type\":\"Point\",
    \"coordinates\":[-7.002648,110.449961]
    }"

let v = decode tw // val v : geo = {t = "Point"; coordinates = [|-7.002648; 110.449961|];}
desco
  • 16,642
  • 1
  • 45
  • 56
  • thank you for your reply, but there are no \" in the string to decode, so i need to find a way to make it work without them (other that tw.Replace("[",@"\"[").Replace("]",@"]\""), thanks ! – jlezard Aug 24 '10 at 12:58
  • [-7.002648,110.449961] is not string value but array of floats, if you fix geo definition so coordinates field is float[] - it should fix this issue. I've corrected my sample to demonstrate this – desco Aug 24 '10 at 13:36
  • @Ronald Wildenberg Accepted ! Thanks – jlezard Aug 25 '10 at 13:59
  • Small side note regarding how to write the `tw` definition without acquiring eye cancer: Use `"""` around your string literal and you can remove all the `\\` stuff and have plain old JSON. – BitTickler Jul 06 '14 at 16:41