Giter Site home page Giter Site logo

fsharp.json's Introduction

FSharp.Json: JSON Serialization Library

FSharp.Json is F# JSON serialization library based on Reflection it's written in F# for F#.

Basic Usage Example

Here's basic example of FSharp.Json usage:

#r "FSharp.Json.dll"
open FSharp.Json

// Your record type
type RecordType = {
    stringMember: string
    intMember: int
}

let data: RecordType = { stringMember = "The string"; intMember = 123 }

// serialize record into JSON
let json = Json.serialize data
printfn "%s" json
// json is """{ "stringMember": "The string", "intMember": 123 }"""

// deserialize from JSON to record
let deserialized = Json.deserialize<RecordType> json
printfn "%A" deserialized
// deserialized is {stringMember = "some value"; intMember = 123;}

Table of Contents

Why?

Why we need yet another F# JSON serialization library? Well, if you happy with the library that you are using currently then probably you do not need another one. There are several available options to choose from:

While all of these libraries do provide some good functionality all of them seem to have a weakness. Some of them are written in C# without out of the box support for F# types. Additionally null safety is alien concept in C# however it is a must in F#. Other libraries provide only low-level functionality leaving a lot of cumbersome coding work to library user. The type provider library forces developer to define JSON schema in form of json examples which is far from convenient way of defining schemas.

FSharp.Json was developed with these values in mind:

  • Easy to use API
  • Automatic mapping of F# types into JSON
  • Out of the box support for F# types
  • Null safety

How?

FSharp.Json is pure F# library. It uses Reflection to get information about F# types at runtime. The code from FSharp.Data library is used for parsing JSON. The JsonValue type is internalized in the FSharp.Json library. The core of FSharp.Json library is located in single Core.fs file.

Documentation

This document describe all details of FSharp.Json library. The source code also has thorough documentation in comments to main types. Each feature of FSharp.Json is thoroughly covered by unit tests.

API Overview

Most of API functions are defined in Json module.

Easiest way to serialize is to call Json.serialize function. It serializes any supported F# type to string containing JSON.

Similarly easiest way to deserialize is to call Json.deserialize<'T> function. It takes string containing JSON and returns instance of type 'T.

Advanced functions

Functions Json.serialize and Json.deserialize are using default configuration. Whenever custom configuration should be used following functions are useful:

  • Json.serializeEx
  • Json.deserializeEx<'T>

Prefix Ex stands for "extended". Both of these functions take JsonConfig instance as a first parameter.

Configuration

JsonConfig represents global configuration of serialization. There's convenient way to override default configuration by using JsonConfig.create function. All parameters of the function are optional and those that are provided override default values.

For example, custom jsonFieldNaming could be found here.

Unformatted JSON

Some products like Apache Spark require unformatted JSON in a single line. It is usefull to produce unformatted single line JSON in some other scenarios. There is a function to produce unformatted JSON: Json.serializeU. U stands for "unformatted". It has the same signature as Json.serialize function. The function is a shorthand to using unformatted member on JsonConfig.

Supported Types

Here's full list of F# types that are supported by FSharp.Json library.

F# Type JSON Type
sbyte
byte
int16
uint16
int
uint
int64
uint64
bigint
single
float
decimal
number
string string
char string
bool bool
DateTime string according to ISO 8601
number epoch time using transform
DateTimeOffset string according to ISO 8601
number epoch time using transform
Guid string
Uri string using transform
Enum string enum value name
number enum value
read Enums section
option option is not represented by itself
None value might be represented as null or omitted
read more in Null Safety section
tuple list
record object
map object
array
list
list
union object with special structure
read more in Unions section
obj read Untyped Data section

Customizing JSON Fields Names

When record type instance is serialized into JSON members names are used as JSON fields names. In some scenarios the JSON should have different fields names then F# record type. This section describes how FSharp.Json library provides JSON customization abilities.

Change JSON field name

JSON field name could be easily customized with JsonField attribute:

#r "FSharp.Json.dll"
open FSharp.Json

// attribute stringMember with JsonField
type RecordType = {
    [<JsonField("different_name")>]
    stringMember: string
    intMember: int
}

let data: RecordType = { stringMember = "The string"; intMember = 123 }

let json = Json.serialize data
printfn "%s" json
// json is """{ "different_name": "The string", "intMember": 123 }"""
// pay attention that "different_name" was used as JSON field name

let deserialized = Json.deserialize<RecordType> json
printfn "%A" deserialized
// deserialized is {stringMember = "some value"; intMember = 123;}

In example above JsonField attribute affects both serialization and deserialization.

Change all fields names

What if all fields names should be different in JSON compared to F# member names? This could be needed for example if naming convention used in F# does not match JSON naming convention. FSharp.Json allows to map fields names with naming function.

In the example below all camel case F# record members are mapped into snake case JSON fields:

#r "FSharp.Json.dll"
open FSharp.Json

// attribute stringMember with JsonField
type RecordType = {
    stringMember: string
    intMember: int
}

let data: RecordType = { stringMember = "The string"; intMember = 123 }

// create config with JSON field naming setting
let config = JsonConfig.create(jsonFieldNaming = Json.snakeCase)

let json = Json.serializeEx config data
printfn "%s" json
// json is """{ "string_member": "The string", "int_member": 123 }"""
// pay attention that "string_member" and "int_member" are in snake case

let deserialized = Json.deserializeEx<RecordType> config json
printfn "%A" deserialized
// deserialized is {stringMember = "some value"; intMember = 123;}

The Json module defines two naming functions that could be used out of the box: snakeCase and lowerCamelCase. The one can define it's own naming rule - it's just a function string -> string.

Null Safety

FSharp.Json is null safe. This means that library will never deserialize JSON into anything with null value. FSharp.Json treats option types as an instruction that value could be null in JSON. For example member of type string can't get null value in FSharp.Json, however string option member can have None value which is translated into null in JSON. Same logic applies to all types supported by FSharp.Json library. See examples in sections below.

Deserialization of null

In the example below stringMember member can't be assigned to null even though F# allows string to be null:

#r "FSharp.Json.dll"
open FSharp.Json

type RecordType = {
    stringMember: string
}

let json = """{"stringMember":null}"""

// this attempt to deserialize will throw exception 
let deserialized = Json.deserialize<RecordType> json

What if the member stringMember could be null in JSON? In such case the record type should explictly use string option type:

#r "FSharp.Json.dll"
open FSharp.Json

type RecordType = {
    stringMember: string option
}

let json = """{"stringMember":null}"""

// this attempt to deserialize will work fine
let deserialized = Json.deserialize<RecordType> json

// deserialized.stringMember equals to None
printfn "%A" deserialized

If value could be null or missing in JSON then F# type used for corresponding member should be option.

Customization of null deserialization

What is the difference between missing field in JSON and field assigned to null? By default FSharp.Json library treats both cases as None, however you can customize this logic. Behaviour that is used to treat option types could be configured:

#r "FSharp.Json.dll"
open FSharp.Json

type RecordType = {
    stringMember: string option
}

// this will work fine by default even when option field is missing 
let deserialized1 = Json.deserialize<RecordType> "{}"

printfn "%A" deserialized1
// deserialized1.stringMember equals to None

// create config that require option None to be represented as null
let config = JsonConfig.create(deserializeOption = DeserializeOption.RequireNull)

// this will throw exception since config in use requires null for None deserialization.
let deserialized2 = Json.deserializeEx<RecordType> config "{}"

Serialization of None

If some member of option type has None value it will be serialized into JSON null by default:

#r "FSharp.Json.dll"
open FSharp.Json

type RecordType = {
    stringMember: string option
}

// stringMember has None value
let data = { RecordType.stringMember = None }
let json = Json.serialize data
printfn "%s" json
// json is: """{ "stringMember": null }"""

Customization of None serialization

The one can control how None values are respresented in JSON through config. Example belows shows how to omit members with None values:

#r "FSharp.Json.dll"
open FSharp.Json

type RecordType = {
    stringMember: string option
}

// stringMember has None value
let data = { RecordType.stringMember = None }

// create config that omits null values
let config = JsonConfig.create(serializeNone = SerializeNone.Omit)

let json = Json.serializeEx config data
printfn "%s" json
// json is: """{}"""

Enums

By default enum value is represented as string that is enum member name.

Check example code below:

#r "FSharp.Json.dll"
open FSharp.Json

type NumberEnum =
| One = 1
| Two = 2
| Three = 3

// value will be represented as enum value name in JSON
type TheNumberEnum = {
    value: NumberEnum
}

let data = { TheNumberEnum.value = NumberEnum.Three }

let json = Json.serialize data
// json is """{"value":"Three"}"""

let deserialized = Json.deserialize<TheNumberEnum> json
// data is { TheNumberEnum.value = NumberEnum.Three }

Customizing enum serialization

EnumValue member of JsonField attribute could be used to change serialization of enums. There are two modes supported currently: enum value name and enum value.

Here's an example of custom enum serialization:

#r "FSharp.Json.dll"
open FSharp.Json

type NumberEnum =
| One = 1
| Two = 2
| Three = 3

// value will be represented as enum value in JSON
type TheAttributedNumberEnum = {
    [<JsonField(EnumValue = EnumMode.Value)>]
    value: NumberEnum
}

let data = { TheNumberEnum.value = NumberEnum.Three }

let json = Json.serialize data
// json is """{"value":3}"""

let deserialized = Json.deserialize<TheNumberEnum> json
// data is { TheNumberEnum.value = NumberEnum.Three }

Default enum behaviour

Sometimes it's needed always serialize enum value as it's value. Annotating each member of any enum type would be cumbersome. JsonConfig allows to override default enum behaviour.

Check the example below:

#r "FSharp.Json.dll"
open FSharp.Json

type NumberEnum =
| One = 1
| Two = 2
| Three = 3

// value will be represented as enum value name in JSON
type TheNumberEnum = {
    value: NumberEnum
}

let data = { TheNumberEnum.value = NumberEnum.Three }

// create configuration instructing to serialize enum as enum value
let config = JsonConfig.create(enumValue = EnumMode.Value)

let json = Json.serializeEx config data
// json is """{"value":3}"""
// value was serialized as enum value which is 3

let deserialized = Json.deserializeEx<TheNumberEnum> config json
// data is { TheNumberEnum.value = NumberEnum.Three }

Unions

JSON format does not support any data structure similiar to F# discriminated unions. Though it is still possible to represent union in JSON in some reasonable way. By deafault FSharp.Json serializes union into JSON object with the only one field. Name of the field corresponds to the union case. Value of the field corresponds to the union case value.

Here's some example of default union serialization:

#r "FSharp.Json.dll"
open FSharp.Json

type TheUnion =
| OneFieldCase of string
| ManyFieldsCase of string*int

let data = OneFieldCase "The string"

let json = Json.serialize data
// json is """{"OneFieldCase":"The string"}"""

let deserialized = Json.deserialize<TheUnion> json
// deserialized is OneFieldCase("The string")

Changing union case key

The string that represents union case key could be changed with JsonUnionCase attribute.

See the example below:

#r "FSharp.Json.dll"
open FSharp.Json

// OneFieldCase is attributed to be "case1" in JSON
type TheUnion =
| [<JsonUnionCase(Case="case1")>] OneFieldCase of string
| ManyFieldsCase of string*int

let data = OneFieldCase "The string"

let json = Json.serialize data
// json is """{"case1":"The string"}"""

let deserialized = Json.deserialize<TheUnion> json
// deserialized is OneFieldCase("The string")

Single case union

Single case union is a special scenario. Read here about single case union usage. In such case serializing union as JSON object is overkill. It's more convenient to represent single case union the same way as a wrapped type.

Here's example of single case union serialization:

#r "FSharp.Json.dll"
open FSharp.Json

// Single case union type
type TheUnion = SingleCase of string

type TheRecord = {
    // value will be just a string - wrapped into union type
    value: TheUnion
}

let data = { TheRecord.value = SingleCase "The string" }

let json = Json.serialize data
// json is """{"value":"The string"}"""

let deserialized = Json.deserialize<TheRecord> json
// deserialized is { TheRecord.value = SingleCase "The string" }

Union case without fields

When union case does not have fields then the union value is represented by string value of the case name itself.

Here's example of serialization union case without fields:

#r "FSharp.Json.dll"
open FSharp.Json

// Case NoFieldCase does not have any fields
type TheUnion =
| NoFieldCase
| SingleCase of string

type TheRecord = {
    // value will be a string represting NoFieldCase
    value: TheUnion
}

let data = { TheRecord.value = NoFieldCase }

let json = Json.serialize data
// json is """{"value":"NoFieldCase"}"""

let deserialized = Json.deserialize<TheRecord> json
// deserialized is { TheRecord.value = NoFieldCase }

Union modes

There's union mode that represents union as JSON object with two fields. One field is for case key and another one is for case value. This mode is called "case key as a field value" If this mode is used then names of these two field should be provided through JsonUnion attribute.

See the example below:

#r "FSharp.Json.dll"
open FSharp.Json

// The union will be represented as JSON object with two fields: casekey and casevalue.
[<JsonUnion(Mode = UnionMode.CaseKeyAsFieldValue, CaseKeyField="casekey", CaseValueField="casevalue")>]
type TheUnion =
| OneFieldCase of string
| ManyFieldsCase of string*int

let data = OneFieldCase "The string"

let json = Json.serialize data
// json is """{"casekey":"OneFieldCase", "casevalue":"The string"}"""

let deserialized = Json.deserialize<TheUnion> json
// deserialized is OneFieldCase("The string")

Type Transform

Supported types section maps F# types into JSON types. What if some data needed to be represented as a different type then the default JSON type? If changing type of the member in F# is not an option then type transform can help.

Any data member is translated F# Type -> JSON type by default types mapping. Type Transform is applied in the middle of this translation: F# Type -> Alternative F# Type -> JSON type. Alternative F# Type -> JSON type is still done by default types mapping, type transform is responsible for F# Type -> Alternative F# Type.

The Transforms module contains transforms that are defined by FSharp.Json library. You can define your own transforms by implementing ITypeTransform interface.

DateTime as epoch time

Let's imagine that some DateTime member should be represented as epoch time in JSON. Epoch time is int64 however it is still convenient to work with DateTime in F# code. In such case DateTimeEpoch transform is useful.

Here's an example of DateTimeEpoch transform usage:

#r "FSharp.Json.dll"
open System
open FSharp.Json

// value will be represented as epoch time in JSON
type DateTimeRecord = {
    [<JsonField(Transform=typeof<Transforms.DateTimeEpoch>)>]
    value: DateTime
}

let json = Json.serialize { DateTimeRecord.value = new DateTime(2017, 11, 5, 22, 50, 45) }
// json is """{"value":1509922245}"""

let deserialized = Json.deserialize<DateTimeRecord> json
// deserialized is { DateTimeRecord.value = new DateTime(2017, 11, 5, 22, 50, 45) }

System.Uri as string

This transformer transforms a Uri to/from a string for serialization. On deserialization, the string value is handed to the System.Uri constructor. When the URI string is malformed, a UriFormatException might be thrown.

Example use:

#r "FSharp.Json.dll"
open System
open FSharp.Json

// value will be represented as epoch time in JSON
type UriRecord = {
    [<JsonField(Transform=typeof<Transforms.UriTransform>)>]
    value: Uri
}

let json = Json.serialize { UriRecord.value = Uri("http://localhost:8080/") }
// json is """{"value":"http://localhost:8080/"}"""

let deserialized = Json.deserialize<UriRecord> json
// deserialized is { UriRecord.value = Uri("http://localhost:8080/") }

Transform by default

To be developed....

Untyped Data

Using obj type in F# code is bad code smell. Though FSharp.Json can serialize and deserialize structures without type information. For allowing obj type in serialization/deserialization allowUntyped flag should be set to true on JsonConfig.

Serialization of obj

When serializing obj FSharp.Json uses real run time type.

Check this example:

#r "FSharp.Json.dll"
open FSharp.Json

// Record type with obj member
type ObjectRecord = {
    value: obj
}

// Set string value to obj member
let data = { ObjectRecord.value = "The string" }

// Need to allow untyped data
let config = JsonConfig.create(allowUntyped = true)

let json = Json.serializeEx config data
// json is """{"value": "The string"}"""
// value was serialized as string because it was assigned to string

Deserialization of obj

When deserializing obj FSharp.Json assumes the type from JSON.

See example below:

#r "FSharp.Json.dll"
open FSharp.Json

// Record type with obj member
type ObjectRecord = {
    value: obj
}

// value is assigned to string
let json = """{"value": "The string"}"""

// Need to allow untyped data
let config = JsonConfig.create(allowUntyped = true)

let data = Json.deserializeEx<ObjectRecord> config json
// data is { ObjectRecord.value = "The string" }
// value was deserialized as string because it was string in JSON

Release Notes

Could be found here.

Contributing and copyright

The project is hosted on GitHub where you can report issues, fork the project and submit pull requests. If you're adding a new public API, please also consider adding documentation to this README.

The library is available under Public Domain license, which allows modification and redistribution for both commercial and non-commercial purposes. For more information see the License file in the GitHub repository.

Maintainer(s)

fsharp.json's People

Contributors

danyx23 avatar dbarbashov avatar fastyep avatar gusty avatar mangelmaxime avatar megafinz avatar nicoviii avatar saerosv avatar vilinski avatar vsapronov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fsharp.json's Issues

.net core build?

Would be great rather than have .net core come up with the warning that FSharp.Json was restored using the .NETFramework ...

Can not serialize/deserialize type with "internal" or "private" modifier

Hello. When I try to serialize/deserialize Person, I have an exception

FSharp.Json.JsonSerializationError: 'Failed to serialize, must be one of following types: record, map, array, list, tuple, union. Type is: Person.'

But if I remove internal modifier for Person, the code works fine


type internal Person = {

    [<JsonField("name")>]
    Name: string

    [<JsonField("age")>]
    Age: int
}

[<EntryPoint>]
let main argv =
    let person = { Name = "John"; Age = 42 }
    let json = Json.serialize person
    0

Production ready?

Just came across this on SO, and this library seems to have much of what I want from an F# JSON serialization library. I notice, however, that it's just gotten started, so I'm very reluctant to use this for any kind of serious work.

How production ready is this? I'm not just talking about how feature-complete or bug-free it is, but also the stability of the API, and not least the stability of serialization/deserialization. Specifically, if I use this library to persist data somewhere, then I want to be able to rehydrate it a month or a year down the line without errors using a new version of the library.

Deserialization works in program but fails in interactive

I've found a strange issue where deserialization of a type works fine in my program, but fails when I run the same serialization in interactive. In interactive I get the following exception:

Couldn't parse config, error=JSON Path: . Failed to serialize, must be one of following types: record, map, array, list, tuple, union. Type is: Configuration.

I haven't been able to create a minimal example of the issue, but I've made a branch on the affected project to demonstrate the issue here.

To see the issue:

  1. Place a debug breakpoint here. This is where we try to parse the Configuration type.
  2. Run the Lint project test. This will try to lint the provided project, eventually hitting the parseConfig breakpoint above, when it tries to parse the default configuration file. It should succeed.
  3. Run the FSharp.Json.Error.fsx file in interactive debug. You should get the exception above, when it tries to parse the same default configuration.

Through this I was able to confirm that the two functions seem to be receiving the same input.

Represent single-case unions as their wrapped type

Single-case DUs are frequently used for added type safety in F#. Since this is such a common pattern, any F# serialization library worth its salt should be able to represent single-case unions as their wrapped type. IMHO this should be the default behavior, but it should be configurable both globally and for specific types.

Pardon if your library already does this - I read through the documentation and couldn't t find any mention of it.

Support null for Map<string, obj>

I'm trying to serialize/deserialize a value of type Map<string, obj>, using the allowUntyped config already.

Everything is fine except when some obj is null. For example, deserializing { "hello": null } will throw System.NullReferenceException; the same for serializing Map [ ("hello", null) ].

Could you please support this particular case?

uints not supported

JsonDeserializationError: JSON Path: guild. Not supported type: UInt64

Do I need to say more?

Custom enum/union parsing/serialization

Hi, I want to serialize a type such as:

type State =
    | None
    | OverflowedState
    | RestartedState

type Main =
    { State: State
    }

to have the following JSON:

{
    "state": "<none|overflowed_state|restarted_state>"
}

I tried using transforms for this, but they don't seem to get called for enum/union values. Is there any way to do it?

Unable to install

Tried to install but get the following error:

Detected package version outside of dependency constraint: FSharp.Json 0.3.0 requires FSharp.Core (>= 4.0.0.1 && < 4.0.1) but version FSharp.Core 4.2.3 was resolved.

Please support more recent F# versions than 4.0.1. :)

Q: how can I handle the reserved F# keyword 'type' in json string { "type": "abc", "weight": "123" }

When I attempt to create a record for the above json string the F# compiler rejects it with
FS0010 Incomplete structured construct at or before this point in field declaration. Expected identifier or other token.

Here is the record that I created for the above string
type TypeAndWeight = { type: string; weight: string }

Disclaimer: I spent 15+ minutes reading open and closed issues to ensure that this question has not already been answered,

Deserialize union in CaseKeyAsFieldValue mode from JSON with more than 2 fields

Is there any particular reason for this check in deserializeUnion?

if fields.Length <> 2 then
    failDeserialization path <| sprintf "Failed to parse union from record with %i fields, should be 2 fields." fields.Length

I have a JSON with more than two fields but I care just for the key field (union case discriminator) and the value field.

{
    "ignore_this": "yes",
    "ignore_that": "also yes",
    ...,
    "discriminator": "type_1",
    "data": {
        ...
    }
}

FSharp.Json.JsonDeserializationError: Expected type float is incompatible with jvalue: "13672.74"

I'm trying to deserialize into a record somewhat like this:

type ApiResponse = {
    [<JsonField("A Name", Transform=typeof<FloatTransform>)>]
    value : float
}

where FloatTransform is defined as follows:

type FloatTransform() =
    interface ITypeTransform with
        member this.targetType() = typeof<string>
        member this.toTargetType obj =
            let value = unbox<float> obj
            value.ToString() :> obj
        member this.fromTargetType obj =
            match obj with
            | :? string as s -> Convert.ToDouble(s) |> box
            | _ -> raise (System.ArgumentException())

However, when I try to deserialize some json like this:

{
    "A Name": "13405.34"
}

by calling deserialize like this: Json.deserialize<ApiResponse> jsonString, it throws this exception:

FSharp.Json.JsonDeserializationError: Expected type float is incompatible with jvalue: "13672.74"
   at FSharp.Json.JsonValueHelpers.raiseWrongType[a,b](JsonPath path, String typeName, a jvalue)
   at FSharp.Json.Core.deserializeNonOption@298(JsonConfig config, JsonPath path, Type t, JsonField jsonField, JsonValue jvalue)
   at FSharp.Json.Core.deserializeRecord@423(JsonConfig config, JsonPath path, Type t, JsonValue jvalue)
   at FSharp.Json.Core.deserializeNonOption@298(JsonConfig config, JsonPath path, Type t, JsonField jsonField, JsonValue jvalue)
   at Microsoft.FSharp.Collections.ArrayModule.MapIndexed[T,TResult](FSharpFunc`2 mapping, T[] array)
   at FSharp.Json.Core.deserializeArray@388(JsonConfig config, JsonPath path, Type t, JsonValue jvalue)
   at FSharp.Json.Core.deserializeNonOption@298(JsonConfig config, JsonPath path, Type t, JsonField jsonField, JsonValue jvalue)
   at FSharp.Json.Core.deserializeRecord@423(JsonConfig config, JsonPath path, Type t, JsonValue jvalue)
   at FSharp.Json.Json.deserializeEx[T](JsonConfig config, String json)

I put breakpoints in my transform implementation and verified that way that the methods are being called, but I still wind up with this error. I also tried reversing the transform - in other words, I tried both float -> string and string -> float, but have this same problem either way. Why?

Is it possible to parse json with mixed lists?

At first: Thanks for this library!
I'm working with it to parse JSON responses from an API and it really works like a charm. If I correctly create types for the expected response, I can work type-safe with that data.
But now the API sends inconsistent data, I get a list where complex objects and strings are mixed.

It looks like that:
{ "downloads": [ [ "English", {..} ] ] }

Is there some elegant way to handle such mixed lists with this library? I tried different approaches but none worked.

Specifying Default Values As Compile Time Constants

From the JsonConfig Documentation it specifies how default values in types should be used. I am trying to be able to get the record to be populated

type MyType= {
      [<JsonField(DefaultValue ="">]  // Works
      asString: string

      [<JsonField(DefaultValue =[||])>]  // Run time error
      asArray: int[]

      [<JsonField(DefaultValue = [])>]  // Compile Time Error
      asList: int list}

I'm assuming this code is used inside the reflexive record value code. Pulling in the default value to the JsonField so that it is able to give it a default value that way. So the JsonField is being created in the library code.

Giving it in this format requires this value to be a compile time constant. "" is a compile time constant, and so is [||] but [] isn't.

Running it in the array [||] example, I get the following run time error

System.ArgumentException : Object of type 'System.Object[]' cannot be converted to type 'System.Int32[]'.

Trying to build in the example with the list I get the following compile time error

[FS0267] This is not a valid constant expression or custom attribute value

I also can't use the Array.empty and List.empty values, because those are also not compile time constants.

I don't know if there is another recommended way of setting defaults that doesn't have this requirement, but I don't see a way of getting around this issue other the letting list values be optional, allowing for the null case, which is not optimal.

Support for IReadOnlyDictionary<K,V>?

I wonder if it could be supported by the libray, it seems to fail with what I hoped to serialize:

#r "nuget: FSharp.Json"
readOnlyDict [1,2;3,4] |> FSharp.Json.Json.serialize
FSharp.Json.JsonSerializationError: Failed to serialize, must be one of following types: record, map, array, list, tuple, union. Type is: Dictionary`2.
   at FSharp.Json.Core.failSerialization[a](String message)
   at FSharp.Json.Core.serialize(JsonConfig config, Type t, Object value)
   at FSharp.Json.Json.serializeEx(JsonConfig config, Object theobj)
   at FSharp.Json.Json.serialize(Object theobj)
   at <StartupCode$FSI_0012>.$FSI_0012.main@()

JSON5 Support

Hi,

Thank you very much for your library!

Do you plan on enhancing the library with JSON5 syntax? Would you be open for JSON5 features pull requests?

Thanks!

'Non option field is missing' error when deserializing

I'm getting a 'Non option field is missing' error when deserializing. My JSON looks like this:

{
"csvFilePath": "C:\Users\Mike\source\repos\Validator2\data\cars.csv",
"delimiter": ",",
"maxErrors": 2,
"numHeaderLines": 0,
"validationList": [
{
"name": "Year",
"desc": "The year the car was made.",
"fieldType": "integer",
"minLen": 4,
"maxLen": 4,
"minValue": 1930,
"maxValue": 2023
},
{
"name": "Make",
"desc": "The make of the car.",
"fieldType": "string",
"minLen": 0,
"maxLen": 35,
"minValue": "A",
"maxValue": "zzzz"
},
{
"name": "Model",
"desc": "The make of the car.",
"fieldType": "string",
"minLen": 0,
"maxLen": 35,
"minValue": "A",
"maxValue": "zzzz"
},
{
"name": "Description",
"desc": "The make of the car.",
"fieldType": "string",
"minLen": 0,
"maxLen": 35,
"minValue": "A",
"maxValue": "zzzz"
},
{
"name": "price",
"desc": "how much it costs",
"fieldType": "decimal",
"minLen": 0,
"maxLen": 6,
"minValue": 100,
"maxValue": 250000
}
]
}

I'm deserializing into RawParamsRecord:

type RawValidationParams = {
name : string
desc : string option
fieldType: string option
//fieldCode: FieldCode option
minLen : int option
maxLen : int option
minValue : decimal option
maxValue : decimal option
}

type RawParamsRecord = {
csvFilePath : string
delimiter : string
maxErrors : int
numHeaderLines: int
validationList_: RawValidationParams list
}

FSharp.Json.JsonDeserializationError: 'JSON Path: validationList_. Non option field is missing'

Originally some of the fields in the JSON file list (e.g., maxValue) were missing., so I filled in all of the values in the array of records. Since most of these are options I thought that should be OK. I'm at a complete loss as to what could be going on.

Is JSON in JSON possible

Hi is it possible with this serializer to deserialize some json subdocument to a string as is? Just like JObject in Json.NET? I need it to encode a http request content. Otherwise it should be escaped, what by manually filled files is not comfortable

Support System.Uri as a native type

This is probably a bit of a slippery slope because it open the question of how far do you want to go with this, but I think Uris are common enough in serialization types that I think it would be nice if they were supported directly by FSharp.Json (like Guid is atm e.g.). What do you think about it? Would you be willing to accept a PR?

Support expanding the content of an object to the upper layer

For example, I wanted to serialize the following records:

type Ex1 = { Val1: int }
type Ex2 = { Nested: Ex1; Val2: int }
let data = { Nested = { Val1 = 1 }; Val2 = 2 }
let json = Json.serialize data

into such form:

{
	"val1": 1,
	"val2": 2
}

It seemed like that this is impossible to achieve with FSharp.Json for now.
Is it possible to have a [<Expand>] attribute to achieve such task?

I got this idea because I wanted to expand the result of serializing a DU with union case key / value mode, like so:

[<JsonUnion(Mode = UnionMode.CaseKeyAsFieldValue, CaseKeyField="casekey", CaseValueField="casevalue")>]
type DU = | Example of string
type ExRec = { Val: int; Union: DU }
let data = { Val = 1; Union = DU.Example "str" }
let json = Json.serialize data

into such form:

{
	"val": 1,
	"casekey": "Example",
	"casevalue": "str"
}

Minify

I was wondering if there is support for being able to compress/minify the json output from a serializer? (removing the newlines and spacing)

Thanks,
Daniel

Unhandled Exception when deserializing a list of objects

I have a bit of JSON that looks like this:

[
  {
    "userId": 1,
    "id": 1,
    "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
    "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
  },
  {
    "userId": 1,
    "id": 2,
    "title": "qui est esse",
    "body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
  }
]

I'm attempting to deserialize it like this:

type User =
  { userId: int
    id: int
    title: string
    body: string }

// code that fetches a string of json

let deserialized = Json.deserialize<User list> json

I'm getting Unhandled Exception: FSharp.Json.JsonDeserializationError: JSON Path: [100].userId. Non option field is missing.

I'm not sure what this means. I'd guess that the JSON is somehow missing that field. What is the correct step to take if I might expect that to be the case (I don't currently, but what if?)

Serialize unions without fields as string instead of object with empty array?

type LogLevel = Info | Warn | Error
type LogEntry = {Level: LogLevel; Message: string}

shouldn't this be serialized as

{message: "My Message", level: "info"}

instead of

{message: "My Message", level: {"Info": []}} ?

SharpLu.Json does this, but you've went the other way? Is that a conscious decision, or should I make a PR to change it?

Other than that, this library is very promising :)

SharplLu.Json is pretty good too, but the lack of a single-case union serialization support was a dealbreak for me on that one. This library supports that, but lacks the above 🤔

Also, is there a way to use camelCasing instead of PascalCasing in the serialized json? PascalCasing is very unusual in json.

Feature request - ISO8601 date transformer(s)

Hey,
I needed this for interop with another language (scala), there are possibly other betters ways to achieve this, but as every language implements ISO8601 (de-)serialization to/from JSON differently it would be nice to have a couple of the more popular string formats of the standard ready to help people whose goal is interop with other languages/libs. I did something like this but hardcoded the 'Z' as it is commonly used/expected as a shortcut for the 0 UTC offset, but it could be made more generic.

    type ISO8601UTCDate() =
        interface ITypeTransform with
            member x.targetType () = (fun _ -> typeof<string>) ()
            member x.toTargetType value = (fun (v: obj) -> (v :?> DateTime).ToString("yyyy-MM-ddTHH:mm:ssZ") :> obj) value
            member x.fromTargetType value = (fun (v: obj) -> DateTime.Parse(v :?> string) :> obj) value

Is it possible to apply transforms with no attributes?

I have written TimeSpanTransform and for some reason cannot use it as an attribute for properties (e.g. I have tuples, nested json documents etc.). Is it possible to add transforms for all the types with no attributes, in configuration?

Support for serialize non ascii words

Now chinese kanji words serialized to :

������"
�件��,������

Q: how do you decode dictionary keys that vary?

Consider a web request for 1 or more of 3 keys that are known ahead of time - foo, bar, cow.

curl "http://host/getme?key=foo,cow"
{"foo": "val1", "cow": "val2"}   # bar is missing because we didn't ask for it

A typical record type definition won't work because we may or may not get the all the keys in the json reply.
I looked in the docs and the closest match were union (sum) types but are they able to solve this? Not seeing how.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.