反序列化twitter流json字符串F#,如何将'null'定义为适当的值?

时间:2010-09-25 17:18:33

标签: json api f# twitter

如果有人想要记录类型,我会留下以下内容,但我的问题可以问如下。 假设需要向文件写入一个可以为null的记录类型,考虑到以下结果,最好的方法是什么:“类型'Basic'没有'null'作为正确的值”:

type Basic = {Toto:string}

let basicToString b = function
    |null -> "null"
    |record->record.Toto

非常感谢


我有以下代码从twitter流中反序列化推文:

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


  let  makeString numFields = 
    if numFields>1 then 
                seq[for i in 1..(numFields-1)->","]
                |> Seq.fold(fun elem res-> elem+res) ""
    else
        ""



[<DataContract>]
type boundingbox = {
    [<field: DataMember(Name = "type")>]
    t:string
    [<field: DataMember(Name = "coordinates")>]
    coordinates:float array array array
    }
    with 
        member this.ToCsv() = let temp =  string this
                              if temp ="" then 
                                  makeString 2
                              else
                                  this.t+","+"coordinates"




[<DataContract>]
type attributes = {
 [<field: DataMember(Name = "street_adress")>]
 street_adress:string
 }
    with 
        member this.ToCsv() = let temp =  string this
                              if temp ="" then 
                                  makeString 1
                              else
                                  this.street_adress



[<DataContract>]
type place = {
    [<field: DataMember(Name = "country")>]
    country:string
    [<field: DataMember(Name = "url")>]
    url:string
    [<field: DataMember(Name = "attributes")>]
    attributes:attributes 
    [<field: DataMember(Name = "country_code")>]
    country_code:string
    [<field: DataMember(Name = "full_name")>]
    full_name:string
    [<field: DataMember(Name = "name")>]
    name:string
    [<field: DataMember(Name = "id")>]
    id:string
    [<field: DataMember(Name = "bounding_box")>]
    bounding_box:boundingbox
    [<field: DataMember(Name = "pldace_type")>]
    place_type:string
    }
    with 
        member this.toCsv() = let temp =  string this
                              if temp ="" then 
                                  makeString 10
                              else
                                  this.country+","+this.url+","+this.attributes.ToCsv()+","+this.country_code+","+this.full_name+","+this.name+","+this.bounding_box.ToCsv()+","+this.place_type



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

[<DataContract>]
type url = {
    [<field: DataMember(Name = "url")>]
    url:string
    [<field: DataMember(Name = "indices")>]
    indices:int array
    }

[<DataContract>]
type hashtag = {
    [<field: DataMember(Name = "text")>]
    text:string
    [<field: DataMember(Name = "indices")>]
    indices:int array
    }


[<DataContract>]
type user_mention = {
    [<field: DataMember(Name = "indices")>]
    indices:int array
    [<field: DataMember(Name = "screen_name")>]
    screen_name:string
    [<field: DataMember(Name = "name")>]
    name:string
    [<field: DataMember(Name = "id")>]
    id:string
    }

[<DataContract>]
type entities = {
    [<field: DataMember(Name = "urls")>]
    urls:url array
    [<field: DataMember(Name = "hashtags")>]
    hashtags:hashtag array
    [<field: DataMember(Name = "user_mentions")>]
    user_mentions:user_mention array
    }

[<DataContract>]
type user = {
    [<field: DataMember(Name = "time_zone")>]
     time_zone:string
    [<field: DataMember(Name = "profile_sidebar_border_color")>]
     profile_sidebar_border_color:string
    [<field: DataMember(Name = "screen_name")>]
    screen_name:string
    [<field: DataMember(Name = "notifications")>]
     notifications:string
    [<field: DataMember(Name = "listed_count")>]
     listed_count:string
    [<field: DataMember(Name = "profile_background_image_url")>]
     profile_background_image_url:string
    [<field: DataMember(Name = "location")>]
    location:string
    [<field: DataMember(Name = "statuses_count")>]
     statuses_count:string
    [<field: DataMember(Name = "profile_background_color")>]
    profile_background_color:string
    [<field: DataMember(Name = "description")>]
     description:string
    [<field: DataMember(Name = "show_all_inline_media")>]
     show_all_inline_media:string
    [<field: DataMember(Name = "profile_background_tile")>]
     profile_background_tile:string
    [<field: DataMember(Name = "contributors_enabled")>]
     contributors_enabled:string
    [<field: DataMember(Name = "geo_enabled")>]
     geo_enabled:string
    [<field: DataMember(Name = "created_at")>]
     created_at:string
    [<field: DataMember(Name = "profile_text_color")>]
     profile_text_color:string
    [<field: DataMember(Name = "followers_count")>]
    followers_count:string
    [<field: DataMember(Name = "profile_use_background_image")>]
    profile_use_background_image:string
    [<field: DataMember(Name = "url")>]
     url:string
    [<field: DataMember(Name = "friends_count")>]
     friends_count:string
    [<field: DataMember(Name = "profile_link_color")>]
    profile_link_color:string
    [<field: DataMember(Name = "protected")>]
     Protected:string
    [<field: DataMember(Name = "lang")>]
     lang:string
    [<field: DataMember(Name = "verified")>]
     verified:string
    [<field: DataMember(Name = "name")>]
     name:string
    [<field: DataMember(Name = "follow_request_sent")>]
     follow_request_sent:string
    [<field: DataMember(Name = "following")>]
     following:string
    [<field: DataMember(Name = "favourites_count")>]
     favourites_count:string
    [<field: DataMember(Name = "profile_sidebar_fill_color")>]
     profile_sidebar_fill_color:string
    [<field: DataMember(Name = "profile_image_url")>]
    profile_image_url:string
    [<field: DataMember(Name = "id")>]
    id:string
    [<field: DataMember(Name = "utc_offset")>]
     utc_offset:string
}




[<DataContract>]
type tweet = {
    [<field: DataMember(Name = "place")>]
     place:place
    [<field: DataMember(Name = "geo")>]
     geo:geo
    [<field: DataMember(Name = "text")>]
    text:string
    [<field: DataMember(Name = "coordinates")>]
    coordinates:geo
    [<field: DataMember(Name = "retweet_count")>]
     retweet_count:string
    [<field: DataMember(Name = "favorited")>]
     favorited:string
    [<field: DataMember(Name = "source")>]
    source:string
    [<field: DataMember(Name = "contributors")>]
     contributors:int array
    [<field: DataMember(Name = "created_at")>]
    created_at:string
    [<field: DataMember(Name = "in_reply_to_status_id")>]
    in_reply_to_status_id:string
    [<field: DataMember(Name = "in_reply_to_screen_name")>]
    in_reply_to_screen_name:string
    [<field: DataMember(Name = "user")>]
     user:user
    [<field: DataMember(Name = "retweeted")>]
    retweeted:string
    [<field: DataMember(Name = "in_reply_to_user_id")>]
    in_reply_to_user_id:string
    [<field: DataMember(Name = "truncated")>]
     truncated:string
    [<field: DataMember(Name = "id")>]
    tweet_id:string
    [<field: DataMember(Name = "entities")>]
    entities:entities
    }



[<DataContract>]
type fullTweet = {
    [<field: DataMember(Name = "place")>]
     place:place
    [<field: DataMember(Name = "geo")>]
     geo:geo
    [<field: DataMember(Name = "text")>]
    text:string
    [<field: DataMember(Name = "coordinates")>]
    coordinates:geo
    [<field: DataMember(Name = "retweet_count")>]
     retweet_count:string
    [<field: DataMember(Name = "favorited")>]
     favorited:string
    [<field: DataMember(Name = "source")>]
    source:string
    [<field: DataMember(Name = "contributors")>]
     contributors:int array
    [<field: DataMember(Name = "created_at")>]
    created_at:string
    [<field: DataMember(Name = "in_reply_to_status_id")>]
    in_reply_to_status_id:string
    [<field: DataMember(Name="retweeted_status")>]
    retweeted_status:tweet
    [<field: DataMember(Name = "in_reply_to_screen_name")>]
    in_reply_to_screen_name:string
    [<field: DataMember(Name = "user")>]
     user:user
    [<field: DataMember(Name = "retweeted")>]
    retweeted:string
    [<field: DataMember(Name = "in_reply_to_user_id")>]
    in_reply_to_user_id:string
    [<field: DataMember(Name = "truncated")>]
     truncated:string
    [<field: DataMember(Name = "id")>]
    tweet_id:string
    [<field: DataMember(Name = "entities")>]
    entities:entities
    }



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

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

我需要完成的下一步是将每个fullTweet记录写入csv文件,问题是有时某些记录类型的字段为空。例如,当解码json字符串时(下面只是字符串的一小部分):

相对= \\ “nofollow的\\” &GT; TweetDeck的\”,\ “收藏\”:假,\ “in_reply_to_status_id \”:空,\ “实体\”:{\ “#标签\”:[{\ “文\”:\ “frac1 \”,\ “指数\”:[117123]}],\ “user_mentions \”:[],\ “的URL \”:[]},\ “位置\”:空, \ “坐标\”:空,\ “地理\”:空,\ “in_reply_to_user_id \”:空,\“

字段“place”和“in_reply_to_user_id”将为空。

如何处理这个,以便当一个字段为空(例如地方)时,csv行读取“null”(对于记录类型的每个字段)?或者,如果你有更好的方法来处理这个?

非常感谢!!!

(希望有些人会喜欢所有记录类型:))

1 个答案:

答案 0 :(得分:0)

如果我理解你的要求,你只需要一个函数,它将获取一个字符串并返回该字符串,如果它不为null,如果它为null则返回“null”:

let fieldToString = function
    | null -> "null"
    | field -> field