-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
parse Expected JSON #61
Comments
Hi Casper, I was just looking at the code in the repository. SimpleJson does not parse JSON that is directly coming from Did you try to run I see also that your solution is using an older version of Fable (v2.4.6) and latest is v2.13.0 in the 2.x family of versions. There is also Fable 3 but that should not have been the problem because Fable.Remoting and SimpleJson are backward compatible |
@Zaid-Ajaj As you suggested I first tried starting to update to the new fable compiler. However I run into the following issue: Following the upgrade recipe, everything seems to work, only I get error messages from everything related to material ui theme functions like:
|
Is this Fable v2.13.0 or Fable 3? Because Fable 3 might be more complicated to migrate |
@Zaid-Ajaj it's the brand new Fable 3 Nagareyama, I think it is the same issue as: Shmew/Feliz.MaterialUI#59. It seems constants or arrays are translated to functions?: MangelMaxime/fulma-demo#43. I will for now revert back to the js fable setup. As for the original issue. To clarify:
That's the reasoning behind it. I have been looking at your code, but it seems that you use Newtonsoft, with only an extra feature to enable F# maps to be serialized? |
It better to leave as much as possible serialization/deserialization work to Fable.Remoting instead of sending strings back and forth because there are lots of edge cases that have been specifically handled to Just Work across the stack.
I wish it was that simple, the converter handles all the edge cases I mentioned above on a per-type basis. See the implementation here |
And it is not possible to use your converter on the server side explicitly and deserialize on the client side explicitly as well? Seemed like a simple solution to me. Only, I cannot discover how you perform the actual serialization and deserialization in Fable.Remoting. |
I think this is going to work: # load ".paket/load/netstandard2.1/Server/Fable.Remoting.Json.fsx"
open Fable.Remoting.Json
open Newtonsoft.Json
open System.IO
open System.Collections.Concurrent
open System.Text
type MyType =
| EndType of string
| MyType of MyType
let testType =
"test"
|> EndType
|> MyType
let private fableConverter = new FableJsonConverter() :> JsonConverter
let private settings = JsonSerializerSettings(DateParseHandling = DateParseHandling.None)
let private fableSerializer =
let serializer = JsonSerializer()
serializer.Converters.Add fableConverter
serializer
let private jsonEncoding = UTF8Encoding false
let jsonSerialize (o: 'a) (stream: Stream) =
use sw = new StreamWriter (stream, jsonEncoding, 1024, true)
use writer = new JsonTextWriter (sw, CloseOutput = false)
fableSerializer.Serialize (writer, o)
let serialize o =
use stream = new MemoryStream()
use reader = new StreamReader(stream)
jsonSerialize o stream
stream.Position <- 0L
reader.ReadToEnd()
testType
|> serialize
|
It's working thanks. |
Awesome! glad to hear you were able to figure it out 😄 |
When trying to parse this string:
json string
I get the following error:
error message
parse Expected JSON: [["Case",["JString","Category"]],["Fields",["JArray",[["JObject",{"comparer":{},"tree":["MapOne","Case",["JString","RootCategory"]]}],["JObject",{"comparer":{},"tree":["MapNode","Fields",["JArray",[["JArray",[["JObject",{"comparer":{},"tree":["MapNode","Fields",["JArray",[["JObject",{"comparer":{},"tree":["MapNode","Fields",["JArray",[["JObject",{"comparer":{},"tree":["MapNode","Max",["JObject",{"comparer":{},"tree":["MapNode","Fields",["JArray",[["JObject",{"comparer":{},"tree":["MapNode","Fields",["JArray",[["JNumber",1]]],["MapOne","Case",["JString","MaxExcl"]],"MapEmpty",2]}]]],["MapOne","Case",["JString","Some"]],"MapEmpty",2]}],"MapEmpty",["MapOne","Min","JNull"],2]}]]],["MapOne","Case",["JString","Age"]],"MapEmpty",2]}],["JObject",{"comparer":{},"tree":["MapNode","Fields",["JArray",[["JArray",[]]]],["MapOne","Case",["JString","Categories"]],"MapEmpty",2]}]]],["MapOne","Case",["JString","Category"]],"MapEmpty",2]}],["JObject",{"comparer":{},"tree":["MapNode","Fields",["JArray",[["JObject",{"comparer":{},"tree":["MapNode","Fields",["JArray",[["JObject",{"comparer":{},"tree":["MapNode","Max",["JObject",{"comparer":{},"tree":["MapNode","Fields",["JArray",[["JObject",{"comparer":{},"tree":["MapNode","Fields",["JArray",[["JNumber",240]]],["MapOne","Case",["JString","MaxExcl"]],"MapEmpty",2]}]]],["MapOne","Case",["JString","Some"]],"MapEmpty",2]}],"MapEmpty",["MapOne","Min",["JObject",{"comparer":{},"tree":["MapNode","Fields",["JArray",[["JObject",{"comparer":{},"tree":["MapNode","Fields",["JArray",[["JNumber",1]]],["MapOne","Case",["JString","MinIncl"]],"MapEmpty",2]}]]],["MapOne","Case",["JString","Some"]],"MapEmpty",2]}]],2]}]]],["MapOne","Case",["JString","Age"]],"MapEmpty",2]}],["JObject",{"comparer":{},"tree":["MapNode","Fields",["JArray",[["JObject",{"comparer":{},"tree":["MapNode","Fields",["JArray",[["JObject",{"comparer":{},"tree":["MapNode","MaxWeightKg","JNull",["MapNode","Indication",["JString","hiv"],["MapNode","Gender",["JObject",{"comparer":{},"tree":["MapNode","Fields",["JArray",[["JString",""]]],["MapOne","Case",["JString","Unknown"]],"MapEmpty",2]}],["MapNode","AbsMaxDose",["JObject",{"comparer":{},"tree":["MapNode","Fields",["JArray",[["JObject",{"comparer":{},"tree":["MapNode","Fields",["JArray",[["JNumber",600]]],["MapOne","Case",["JString","Quantity"]],"MapEmpty",2]}]]],["MapOne","Case",["JString","Some"]],"MapEmpty",2]}],"MapEmpty",["MapOne","Freqs",["JArray",[["JObject",{"comparer":{},"tree":["MapNode","Time",["JObject",{"comparer":{},"tree":["MapNode","Item2",["JString","dag"],["MapOne","Item1",["JNumber",1]],"MapEmpty",2]}],["MapOne","Count",["JNumber",1]],"MapEmpty",2]}],["JObject",{"comparer":{},"tree":["MapNode","Time",["JObject",{"comparer":{},"tree":["MapNode","Item2",["JString","dag"],["MapOne","Item1",["JNumber",1]],"MapEmpty",2]}],["MapOne","Count",["JNumber",2]],"MapEmpty",2]}]]]],2],["MapOne","Generic",["JString","abacavir"]],3],["MapNode","MaxGestAgeDays","JNull",["MapNode","MaxDose","JNull",["MapOne","MaxAgeMo",["JObject",{"comparer":{},"tree":["MapNode","Fields",["JArray",[["JNumber",240]]],["MapOne","Case",["JString","Some"]],"MapEmpty",2]}]],"MapEmpty",2],["MapNode","MaxPerDose","JNull",["MapOne","MaxPMAgeDays","JNull"],"MapEmpty",2],3],4],["MapNode","Route",["JString","or"],["MapNode","MinWeightKg","JNull",["MapNode","MinGestAgeDays","JNull",["MapNode","MinDose","JNull",["MapOne","MinAgeMo",["JObject",{"comparer":{},"tree":["MapNode","Fields",["JArray",[["JNumber",1]]],["MapOne","Case",["JString","Some"]],"MapEmpty",2]}]],"MapEmpty",2],["MapOne","MinPMAgeDays","JNull"],3],["MapNode","Products",["JArray",[]],["MapOne","NormDose",["JObject",{"comparer":{},"tree":["MapNode","Fields",["JArray",[["JObject",{"comparer":{},"tree":["MapNode","Fields",["JArray",[["JNumber",16]]],["MapOne","Case",["JString","QuantityPerKg"]],"MapEmpty",2]}]]],["MapOne","Case",["JString","Some"]],"MapEmpty",2]}]],"MapEmpty",2],4],["MapNode","Specialty",["JString",""],["MapOne","Shape",["JString","drank"]],["MapNode","StartDose","JNull","MapEmpty",["MapOne","Unit",["JString","mg"]],2],3],5],6]}]]],["MapOne","Case",["JString","Some"]],"MapEmpty",2]}]]],["MapOne","Case",["JString","Dose"]],"MapEmpty",2]}]]],["MapOne","Case",["JString","Category"]],"MapEmpty",2]}],["JObject",{"comparer":{},"tree":["MapNode","Fields",["JArray",[["JObject",{"comparer":{},"tree":["MapNode","Fields",["JArray",[["JObject",{"comparer":{},"tree":["MapNode","Max","JNull","MapEmpty",["MapOne","Min",["JObject",{"comparer":{},"tree":["MapNode","Fields",["JArray",[["JObject",{"comparer":{},"tree":["MapNode","Fields",["JArray",[["JNumber",240]]],["MapOne","Case",["JString","MinIncl"]],"MapEmpty",2]}]]],["MapOne","Case",["JString","Some"]],"MapEmpty",2]}]],2]}]]],["MapOne","Case",["JString","Age"]],"MapEmpty",2]}],["JObject",{"comparer":{},"tree":["MapNode","Fields",["JArray",[["JArray",[]]]],["MapOne","Case",["JString","Categories"]],"MapEmpty",2]}]]],["MapOne","Case",["JString","Category"]],"MapEmpty",2]}]]]]],["MapOne","Case",["JString","Categories"]],"MapEmpty",2]}]]]]] to match the type [{"CaseName":"Category","CaseTypes":[["Union",null],["Union",null]],"Info":{"declaringType":{"fullname":"Informedica.Formulary.Shared.Types.Category","generics":[]},"tag":0,"name":"Category","fields":[["Item1",{"fullname":"Informedica.Formulary.Shared.Types.PatientCategory","generics":[]}],["Item2",{"fullname":"Informedica.Formulary.Shared.Types.CategoriesOrDose","generics":[]}]]}}]
On the server side I use Newtonsoft to create a json string:
On the client side I use SimpleJson to deserialize:
See this repo: https://github.com/halcwb/GenPedForm/blob/420b91c173fc5e9aec2ea9964abe0b2198830267/src/Informedica.Formulary.Client/App.fs#L129.
The text was updated successfully, but these errors were encountered: