我无法将Java / JSON地图转换为可用的F#对象。
这是我的代码的核心:
member this.getMapFromRpcAsynchronously =
Rpc.getJavaJSONMap (new Action<_>(this.fillObjectWithJSONMap))
()
member this.fillObjectWithJSONMap (returnedMap : JSONMap<string, int> option) =
let container = Option.get(returnedMap)
let map = container.map
for thing in map do
this.myObject.add thing.key
// do stuff with thing
()
我的RPC方法返回的JSON如下所示:
{"id":1, "result":
{"map":
{"Momentum":12, "Corporate":3, "Catalyst":1},
"javaClass":"java.util.HashMap"}
}
我正在尝试将其映射到F#DataContract,如下所示:
[<DataContract>]
type JSONMap<'T, 'S> = {
[<DataMember>]
mutable map : KeyValuePair<'T, 'S> array
[<DataMember>]
mutable javaClass : string
}
[<DataContract>]
type JSONSingleResult<'T> = {
[<DataMember>]
mutable javaClass: string
[<DataMember>]
mutable result: 'T
}
最后,执行实际RPC调用的F#方法(上面的Rpc.getJavaJSONMap)如下所示:
let getJavaJSONMap (callbackUI : Action<_>) =
ClientRpc.getSingleRPCResult<JSONSingleResult<JSONMap<string, int>>, JSONMap<string, int>>
"MyJavaRpcClass"
"myJavaRpcMethod"
"" // takes no parameters
callbackUI
(fun (x : option<JSONSingleResult<JSONMap<string, int>>>) ->
match x.IsSome with
| true -> Some(Option.get(x).result)
| false -> None
)
在编译时我没有错误。调用我的RPC方法,并返回一个结果(使用Fiddler查看实际的调用和返回)。但是,看起来F#在将JSON匹配到我的DataContract时遇到了麻烦,因为最顶端的returnedMap始终为null。
任何想法或建议都将不胜感激。谢谢。
答案 0 :(得分:2)
这是我做的东西:
open System.Web.Script.Serialization // from System.Web.Extensions assembly
let s = @"
{""id"":1, ""result"":
{""map"":
{""Momentum"":12, ""Corporate"":3, ""Catalyst"":1},
""javaClass"":""java.util.HashMap""}
}
"
let jss = new JavaScriptSerializer()
let o = jss.DeserializeObject(s)
// DeserializeObject returns nested Dictionary<string,obj> objects, typed
// as 'obj'... so add a helper dynamic-question-mark operator
open System.Collections.Generic
let (?) (o:obj) name : 'a = (o :?> Dictionary<string,obj>).[name] :?> 'a
printfn "id: %d" o?id
printfn "map: %A" (o?result?map
|> Seq.map (fun (KeyValue(k:string,v)) -> k,v)
|> Seq.toList)
// prints:
// id: 1
// map: [("Momentum", 12); ("Corporate", 3); ("Catalyst", 1)]
答案 1 :(得分:1)
这是一个复杂的问题。我假设这个:
{"map":
{"Momentum":12, "Corporate":3, "Catalyst":1},
"javaClass":"java.util.HashMap"}
可能包含可变数量的字段。并且在JSON表示法中转换为对象(javascript对象基本上(或非常类似于)映射)。我不知道这是否会直接翻译成F#。
可能会阻止F#静态类型与javascript的动态类型不允许。
您可能必须自己编写转换例程。
好的,数据契约中有一些小错误让我们重新定义JsonMap并删除“javaclass”属性,因为它不是在提供的JSON示例中(它是更高级别的),它看起来好像对我来说keyvaulepair不是序列化,所以我们定义我们自己的类型:
type JsonKeyValuePair<'T, 'S> = {
[<DataMember>]
mutable key : 'T
[<DataMember>]
mutable value : 'S
}
type JSONMap<'T, 'S> = {
[<DataMember>]
mutable map : JsonKeyValuePair<'T, 'S> array
}
并创建反序列化函数:
let internal deserializeString<'T> (json: string) : 'T =
let deserializer (stream : MemoryStream) =
let jsonSerializer
= Json.DataContractJsonSerializer(
typeof<'T>)
let result = jsonSerializer.ReadObject(stream)
result
let convertStringToMemoryStream (dec : string) : MemoryStream =
let data = Encoding.Unicode.GetBytes(dec);
let stream = new MemoryStream()
stream.Write(data, 0, data.Length);
stream.Position <- 0L
stream
let responseObj =
json
|> convertStringToMemoryStream
|> deserializer
responseObj :?> 'T
let run2 () =
let json = "{\"map@\":[{\"key@\":\"a\",\"value@\":1},{\"key@\":\"b\",\"value@\":2}]}"
let o = deserializeString<JSONMap<string, int>> json
()
我能够将字符串反序列化为适当的对象结构。我希望看到的两件事是
1)为什么.NET强迫我在字段名后附加@字符? 2)进行转换的最佳方法是什么?我猜想一个表示JSON结构的抽象语法树可能是要走的路,然后将其解析为新的字符串。我对AST及其解析并不是很熟悉。
也许其中一位F#专家可以提供帮助或提出更好的翻译方案?
最后在结果类型中添加:
[<DataContract>]
type Result<'T> = {
[<DataMember>]
mutable javaClass: string
[<DataMember>]
mutable result: 'T
}
和转换映射函数(在这种情况下有效 - 但有许多弱点,包括递归映射定义等):
let convertMap (json: string) =
let mapToken = "\"map\":"
let mapTokenStart = json.IndexOf(mapToken)
let mapTokenStart = json.IndexOf("{", mapTokenStart)
let mapObjectEnd = json.IndexOf("}", mapTokenStart)
let mapObjectStart = mapTokenStart
let mapJsonOuter = json.Substring(mapObjectStart, mapObjectEnd - mapObjectStart + 1)
let mapJsonInner = json.Substring(mapObjectStart + 1, mapObjectEnd - mapObjectStart - 1)
let pieces = mapJsonInner.Split(',')
let convertPiece state (piece: string) =
let keyValue = piece.Split(':')
let key = keyValue.[0]
let value = keyValue.[1]
let newPiece = "{\"key\":" + key + ",\"value\":" + value + "}"
newPiece :: state
let newPieces = Array.fold convertPiece [] pieces
let newPiecesArr = List.toArray newPieces
let newMap = String.Join(",", newPiecesArr)
let json = json.Replace(mapJsonOuter, "[" + newMap + "]")
json
let json = "{\"id\":1, \"result\": {\"map\": {\"Momentum\":12, \"Corporate\":3, \"Catalyst\":1}, \"javaClass\":\"java.util.HashMap\"} } "
printfn <| Printf.TextWriterFormat<unit>(json)
let json2 = convertMap json
printfn <| Printf.TextWriterFormat<unit>(json2)
let obj = deserializeString<Result<JSONMap<string,int>>> json2
它仍然存在于各个地方的@符号 - 我没有得到......
为&符号添加转换w / workaround
let convertMapWithAmpersandWorkAround (json: string) =
let mapToken = "\"map\":"
let mapTokenStart = json.IndexOf(mapToken)
let mapObjectEnd = json.IndexOf("}", mapTokenStart)
let mapObjectStart = json.IndexOf("{", mapTokenStart)
let mapJsonOuter = json.Substring(mapTokenStart , mapObjectEnd - mapTokenStart + 1)
let mapJsonInner = json.Substring(mapObjectStart + 1, mapObjectEnd - mapObjectStart - 1)
let pieces = mapJsonInner.Split(',')
let convertPiece state (piece: string) =
let keyValue = piece.Split(':')
let key = keyValue.[0]
let value = keyValue.[1]
let newPiece = "{\"key@\":" + key + ",\"value@\":" + value + "}"
newPiece :: state
let newPieces = Array.fold convertPiece [] pieces
let newPiecesArr = List.toArray newPieces
let newMap = String.Join(",", newPiecesArr)
let json = json.Replace(mapJsonOuter, "\"map@\":[" + newMap + "]")
json
let json = "{\"id\":1, \"result\": {\"map\": {\"Momentum\":12, \"Corporate\":3, \"Catalyst\":1}, \"javaClass\":\"java.util.HashMap\"} } "
printfn <| Printf.TextWriterFormat<unit>(json)
let json2 = convertMapWithAmpersandWorkAround json
printfn <| Printf.TextWriterFormat<unit>(json2)
let obj = deserialize<Result<JSONMap<string,int>>> json2
中加入:
[<DataContract>]
记录上方的修复了Ampersand问题。