This piece of code:
open FSharp.Data
type ColorProvider = JsonProvider<"""
[
{
"color": "Red",
"code": 15
},
{
"color": "Green"
}
]
""", SampleIsList=true>
let value = ColorProvider.Root(color = "Blue", code = None)
printf "%A" value
Produces this JSON:
{
"color": "Blue",
"code": null
}
I'm passing the json to an external service which doesn't handle nulls. Either it must be an integer or the whole property must be missing.
Is there a way to hide the code property when it's null?
You are using FSharp's object printing feature for generating JSON. It produces JSON-like output, but in my opinion it is not meant for that task. I'd suggest using a JSON library for that, personally I like using https://github.com/Microsoft/fsharplu/wiki/fsharplu.json for that task, as it produces beautiful JSON for Discriminate Unions.
Related
Hello I am beginner and trying to understand how to send an array with objects. Does the server understand what an array is like Int, Strings or Booleans? Do you have to send the array in a string for JSON? What I do not understand?
var productsResult = ""
let encoder = JSONEncoder()
let productObject = ProductUsed(name: "Custom name", reason: "This Reason", apply_way: "Intravenous infusion", dosing: "2x", date_start: "22-02-1999", date_end: "22-03-2003")
do {
let result = try encoder.encode([productObject])
if let resultString = String(data: result, encoding: .utf8) {
print(resultString)
productsResult = resultString
}
} catch {
print(error)
}
json["products_used"] = productsResult
And I sent to server with parameters like this:
parameters: ["pregnancy_week": 0, "body_height": 198, "initials": "John Appleseed", "heavy_effect": false, "sex": "Male", "pregnancy": false, "month_of_birth": 3, "reaction": "No option checked", "additional_info": "Eeee", "products_used": "[{\"date_end\":\"22-03-2003\",\"dosing\":\"2x\",\"date_start\":\"22-02-1999\",\"apply_way\":\"Intravenous infusion\",\"name\":\"Custom name\",\"reason\":\"This Reason\"}]", "description": "Eeee", "result": "Recovery without lasting consequences", "year_of_birth": 1983, "day_of_birth": 11, "issue_date": "15-11-2020", "body_weight": 78]
but printed "resultString" in log and looks good...
[{"date_end":"22-03-2003","dosing":"2x","date_start":"22-02-1999","apply_way":"Intravenous infusion","name":"Custom name","reason":"This Reason"}]
What's wrong in my code and why I have " \ " between words in "products_used" key?
JSON, unlike XML, does not specify the structure and type explicitly. This means that the server must know what JSON data to expect.
In JSON there are a few value types (https://www.w3schools.com/js/js_json_syntax.asp):
a string
a number
an array
a boolean
null
a JSON object (a dictionary with tags like { "first" : "John", "last" : "Doe" }). This allows nesting.
A JSON object is a set of tag-value pairs. The tag and value are separated by : and pairs are separated by ,.
An array is a list of JSON values. So for example [ "hello", world" ] is a JSON array with 2 strings and [ 12, 54 ] is a JSON array with two numbers.
Your parameter list ["pregnancy_week": 0, "body_height": 198, ... is not an array but a dictionary instead. A Swift dictionary is translated to an JSON object, not a JSON array.
The \ you see printed acts as escape character. This escape character is used to allow you to have a " inside the string.
That's just a few things that I hope will help understand things a bit better. Your questions are pretty basic, which is fine and it's great you want to understand things. But instead of us explaining everything here, I think it would be best if you'd read about the structure of JSON and how JSON works in Swift on your own.
Cosider an API which reply is always of this structure:
{
"pagination": {
"limit": int,
"offset": int,
"count": int,
"total": int
},
"data": [
{...some obj...}
]
}
So payloads differ only in structure of data objects.
Ideally I'd like to tell F# that all types built from samples have some common part - pagination info, so I can have one generic method which reads all pages.
Is it possible, or do I have to extract pagination object and data array separately with two type providers? I see the benefit of having one provider per response body as it supports reading data from the stream.
I would define two different provided types, one for parsing the pagination data and one for parsing the actual data, i.e. something like this:
type Pagination = JsonProvider<"""{
"pagination": { "limit": 1, "offset": 2,
"count": 3, "total": 4 }
}""">
type OneDataType = JsonProvider<"""{
"data": [ {"a": 1} ]
}""">
If you want to avoid parsing the same JSON file twice (e.g. by calling Pagination.Parse and OneDataType.Parse on the same string), you can actually just parse the data once and then pass the parsed JsonValue to the other type:
let odt = OneDataType.Load("/some/file")
let pg = Pagination.Root(odt.JsonValue)
pg.Pagination.Count
If you wanted to do this with a single provided type, then you could define multiple different fields for the multiple different types of data - but you'd have to name those differently. You'd then need to do some fiddling to read the data correctly. I would not do this, because I find it confusing, but it would look something like this:
type AnyData = JsonProvider<"""{
"pagination": { "limit": 1, "offset": 2,
"count": 3, "total": 4 },
"data": [],
"one_data_type": [ {"a":1} ],
"another_data_type": [ {"b":"xx" }]
}""">
let a = AnyData.Load("/some/file")
// Access pagination data
a.Pagination
// Access data as if it was OneDataType
let oneData = [| for d in a.Data ->
AnyData.OneDataType(d.JsonValue) |]
I'm using the FSharp.Data.TypeProvider but I have an optional property that is not rendered as such.
I have this example JSON:
[
{
"error": {
"message": "Error validating access token: Session has expired on Friday, 24-Jul-20 16:00:00 PDT. The current time is Friday, 24-Jul-20 16:06:14 PDT.",
"type": "OAuthException",
"code": 190,
"error_subcode": 463,
"fbtrace_id": "A0yQG8l8ggauD5PMbYSnEyA"
}
},
{
"error": {
"message": "Error validating access token: Session has expired on Friday, 24-Jul-20 16:00:00 PDT. The current time is Friday, 24-Jul-20 16:06:14 PDT.",
"type": "OAuthException",
"code": 190,
"error_subcode": 463,
"fbtrace_id": "A0yQG8l8ggauD5PMbYSnEyA"
}
},
{
"data": [
{
"id": "17841511906082530"
}
]
}
]
The field error is read as Optional<Error> meanwhile the field data is Datum [], It should be Optional<Data>, this error seems to have something to do with the array, if I change the value of data to another type, like a number, it correctly infers the type.
Maybe is some cache that's wrong? How could I reset the TypeProvider cache?
OR am I doing something else wrong?
This issue is also reported in the github project: https://github.com/fsharp/FSharp.Data/issues/1322
That's expected behavior.
In your JSON string, data is an array, an empty array indicates None, thus the field Data is read as array. That makes sense.
The fact that each element of that array belongs to type Datum also makes sense because Datum is the single form of Data (plural form). Indeed, if you rename data to dogs you will see it is read as Dog [].
Update: I don’t think we can tell the type provider to infer the field data as Option<Datum[]>. To check whether it is actually an empty array or not existed, we need to examine the JsonValue field:
match node.JsonValue with
| JsonValue.Record [|("data", _)|] ->
// yep, we have field data here
| _ ->
// nope, field data is missed
I am struggling a little bit to use DDMathParser framework for expression requirement I have. I have JSON of fields & based on expressions certain fields can be set required, hidden or set the value of it.
Expressions in required tag in sample JSON are not fixed & so not getting how to achieve dynamic approach for expression.
[
{
"name": "firstName",
"value": "Ameer",
**"required": true**
},
{
"name": "lastName",
"value": "Shaikh",
**"required": "$firstName != ‘’"**
},
{
"name": "designation",
"value": "",
**"required": "$firstName == ‘Ameer’ && $lastName == ‘Shaikh’"**
},
{
"name": "location",
"value": "",
**"hidden": false**
}
]
Actually, expression in JSON contains $ to represent one of the
dictionary objects value from JSON. Wherein framework internally
treats it as a local variable.
These expressions may have different combinations as well. There may be several expression apart from "required" parameters. I need to run all relevant expressions for any change in value in UI.
EDIT
let expression = "$firstName == ‘Ameer’ && $lastName == ‘Shaikh’"
let dict = ["firstName": "Amir", "lastName": ""]
let e = try! Expression(string: expression)
let result = try! Evaluator.default.evaluate(e, substitutions: dict)
Though it should parse a correct value from JSON, I have hard coded substitutions string to at least get a breakthrough. Here, substitutions expect String & Double & give error as "Cannot convert a value of type [String: String] to expected arg type substitutions (Dcitionary).
Is there any way to pass String: String substitutions?
DDMathParser is not built to do string evaluations. It's technically possible to make it work, but it's a bit beyond the scope of the framework.
For this situation, I think you'd be better off using NSPredicate, which does allow string comparisons and variable substitutions.
I noticed some strange output from Newtonsoft.Json today, I'm not sure if it is an interaction with F# types or something that can occur in C# as well, so I've tagged both. I have a list of the following record being serialized:
type SplitTracker =
{
[<JsonIgnore>]
split : SplitDefinition
mutable start : duration
mutable ``end`` : duration
mutable lapCount : int
mutable duration : duration Option
}
I serialize it with JsonConvert.SerializeObject and I get the following odd output:
"splits": [
{
"start#": "0.00",
"end#": "0.00",
"lapCount#": 0,
"duration#": null,
"start": "0.00",
"end": "0.00",
"lapCount": 0,
"duration": null
},
{
"start#": "0.00",
"end#": "0.00",
"lapCount#": 0,
"duration#": null,
"start": "0.00",
"end": "0.00",
"lapCount": 0,
"duration": null
}
Anyone know why that might be happening? The data is correct, the duplication of fields with the "#" symbol is the issue.
The way you have defined your record is the culprit here. Record fields are exposed as properties - but you are using mutable properties. F# will turn that into a class that has fields for each of your mutables (the name is the property name, prefixed with #), and properties that read out those.
Json will now attempt to serialize all fields and all properties - hence you get the duplication.
Try it out in F# interactive:
type SplitTracker =
{
mutable start : float
}
let t = typeof<SplitTracker>
let fields1 = t.GetFields() // This will give you a field '#start'
let props1 = t.GetProperties() // This will give you a property 'start'
Contrast that with what you get when using a plain record:
type SplitTracker2 =
{
start : float
}
let t2 = typeof<SplitTracker2>
let fields2 = t2.GetFields() // You will not see any fields
let props2 = t2.GetProperties() // There is a single property 'start'
This should serialize correctly. Apart from that, it makes your code more idiomatic.