зеркало из https://github.com/microsoft/fsharplu.git
more Json doc, add THIRDPARTYNOTICE and LICENSE from LCA
This commit is contained in:
Родитель
bd9251acba
Коммит
2c0ea86f9b
|
@ -0,0 +1,13 @@
|
|||
FSharpLu
|
||||
|
||||
Copyright (c) Microsoft Corporation
|
||||
|
||||
All rights reserved.
|
||||
|
||||
MIT License
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ""Software""), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
197
README.md
197
README.md
|
@ -7,10 +7,9 @@ security, async, parsing, diagnostics, configuration files and Json serializatio
|
|||
This library is used in serveral projects internally at Microsoft and made available
|
||||
to the F# community
|
||||
|
||||
|
||||
## License
|
||||
|
||||
Apache 2.0
|
||||
[MIT](LICENSE.md)
|
||||
|
||||
## Packages
|
||||
|
||||
|
@ -22,8 +21,22 @@ In particular it provides more succinct serialization for option types and discr
|
|||
## Contact
|
||||
william.blum@microsoft.com
|
||||
|
||||
|
||||
|
||||
## FSharp
|
||||
|
||||
Here is a list of helper modules provided by FSharpLu.
|
||||
- [FSharp.Async](FSharpLu\Async.fs)
|
||||
- [FSharp.Configuration](FSharpLu\Configuration.fs)
|
||||
- [FSharp.Collection](FSharpLu\Collections.fs)
|
||||
- [FSharp.Diagnostics](FSharpLu\Diganostics.fs)
|
||||
- [FSharp.ErrorHandling](FSharpLu\ErrorHandling.fs)
|
||||
- [FSharp.File](FSharpLu\File.fs)
|
||||
- [FSharp.Logger](FSharpLu\Logger.fs)
|
||||
- [FSharp.Option](FSharpLu\Option.fs)
|
||||
- [FSharp.Parsing](FSharpLu\Parsing.fs)
|
||||
- [FSharp.Security](FSharpLu\Security.fs)
|
||||
- [FSharp.Text](FSharpLu\Text.fs)
|
||||
- [FSharp.TraceLogging](FSharpLu\TraceLogging.fs)
|
||||
|
||||
## FSharp.Json
|
||||
|
||||
Newtonsoft's Json.Net is the library of choice for Json serialization. Unfortunately
|
||||
|
@ -35,81 +48,123 @@ To use our serializer open the module with
|
|||
|
||||
open Microsoft.FSharpLu.Json
|
||||
|
||||
Take the following simple value of type `(int option) list`
|
||||
To serialize an object using the default Json.Net format you can use
|
||||
`Default.serialize`.
|
||||
|
||||
[Some 5; None; Some 6]
|
||||
|
||||
It gets serialized by JSON.Net with command `Default.serialize [Some 5; None; Some 6]` as
|
||||
|
||||
``Json
|
||||
[
|
||||
{
|
||||
"Case": "Some",
|
||||
"Fields": [ 5 ]
|
||||
},
|
||||
null,
|
||||
{
|
||||
"Case": "Some",
|
||||
"Fields": [ 6 ]
|
||||
}
|
||||
]
|
||||
``
|
||||
### Option type
|
||||
|
||||
In comparison our seriliazer generates the following Json one-liner:
|
||||
The following examples shows how JSon.net serializes
|
||||
a simple value of type `(int option) list`:
|
||||
|
||||
``Json
|
||||
[ 5, null, 6 ]"
|
||||
``
|
||||
```Json
|
||||
Default.serialize [Some 5; None; Some 6]
|
||||
|
||||
val it : string = "
|
||||
[
|
||||
{
|
||||
"Case": "Some",
|
||||
"Fields": [ 5 ]
|
||||
},
|
||||
null,
|
||||
{
|
||||
"Case": "Some",
|
||||
"Fields": [ 6 ]
|
||||
}
|
||||
]"
|
||||
```
|
||||
|
||||
Using the compact serializer provided by FSharpLu.Json the same objects
|
||||
gets serialized instead as a one-liner heterogeneous array:
|
||||
|
||||
```Fsharp
|
||||
Compact.serialize [Some 5; None; Some 6]
|
||||
|
||||
val it : string = "[ 5, null, 6 ]"
|
||||
```
|
||||
|
||||
### Discriminated unions
|
||||
|
||||
Now let's take a look at simple field-less discriminated unions. The value `Foo` of type `type SimpleDu = Foo | Bar`
|
||||
gets serialized by Json.Net as `{ "Case": "Foo" }` instead of just `"Foo"` by our serializer.
|
||||
Now let's take a look at simple field-less discriminated unions.
|
||||
Take for instance the type `type SimpleDu = Foo | Bar`.
|
||||
The value `Foo` gets serialized by Json.Net as follows:
|
||||
|
||||
| Value | Default (Json.net) | Compact
|
||||
| ----- | ------------------ | ----------
|
||||
| `Foo` | `{ "Case": "Foo" }` | `"Foo"`|
|
||||
|
||||
|
||||
### Discriminated unions with fields
|
||||
|
||||
Our serializer also supports generic discrimnated unions with fields for instance take the following
|
||||
binary Tree example:
|
||||
|
||||
``FSharp
|
||||
type 'a Tree = Leaf of 'a | Node of 'a Tree * 'a Tree
|
||||
let x = Node (Node((Leaf 1), (Leaf 4)), Leaf 6)
|
||||
Default.serialize x
|
||||
Union.serialize x
|
||||
``
|
||||
|
||||
The value `x` gets serialized by Json.net as
|
||||
```FSharp
|
||||
type 'a Tree = Leaf of 'a | Node of 'a Tree * 'a Tree
|
||||
let x = Node (Node((Leaf 1), (Leaf 4)), Leaf 6)
|
||||
```
|
||||
|
||||
``Json
|
||||
{
|
||||
"Case": "Node",
|
||||
"Fields": [
|
||||
{
|
||||
"Case": "Node",
|
||||
"Fields": [
|
||||
{
|
||||
"Case": "Leaf",
|
||||
"Fields": [ 1 ]
|
||||
},
|
||||
{
|
||||
"Case": "Leaf",
|
||||
"Fields": [ 4 ]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"Case": "Leaf",
|
||||
"Fields": [ 6 ]
|
||||
} ]
|
||||
}``
|
||||
Default Json.net serialization:
|
||||
|
||||
where FSharpLu.Json produces the more succinct:
|
||||
|
||||
``Json
|
||||
{
|
||||
"Node": [
|
||||
{
|
||||
"Node": [
|
||||
{ "Leaf": 1 },
|
||||
{ "Leaf": 4 }
|
||||
```FSharp
|
||||
Default.serialize x
|
||||
|
||||
val it : string = "
|
||||
{
|
||||
"Case": "Node",
|
||||
"Fields": [
|
||||
{
|
||||
"Case": "Node",
|
||||
"Fields": [
|
||||
{
|
||||
"Case": "Leaf",
|
||||
"Fields": [ 1 ]
|
||||
},
|
||||
{
|
||||
"Case": "Leaf",
|
||||
"Fields": [ 4 ]
|
||||
}
|
||||
]
|
||||
},
|
||||
{ "Leaf": 6 }
|
||||
]
|
||||
}``
|
||||
},
|
||||
{
|
||||
"Case": "Leaf",
|
||||
"Fields": [ 6 ]
|
||||
} ]
|
||||
}"
|
||||
```
|
||||
|
||||
where FSharpLu.Json produces the more succinct and easier to read:
|
||||
|
||||
```Json
|
||||
Compact.serialize x
|
||||
|
||||
val it : string = "
|
||||
{
|
||||
"Node": [
|
||||
{
|
||||
"Node": [
|
||||
{ "Leaf": 1 },
|
||||
{ "Leaf": 4 }
|
||||
]
|
||||
},
|
||||
{ "Leaf": 6 }
|
||||
]
|
||||
}"```
|
||||
|
||||
### Backward compatibility with Json.Net
|
||||
|
||||
FSharpLu.Json incldues a third serializer called `BackwardCompatible`. While it produces the same
|
||||
Json as the compact serializer it can deserialize Json in both compact format as well as the
|
||||
default format produced by the stock JSon.net serializer.
|
||||
|
||||
This is helpful when migrating projects from Json.Net to FSharpLu.Json as it lets you deserialize Json that
|
||||
was produced by earlier versions of your code.
|
||||
|
||||
Expressed more formally this serializer verifies the following properties:
|
||||
|
||||
- `BackwardCompatible.serialize` = `Compact.serialize`
|
||||
- `Default.serialize >> BackwardCompatible.deserialize` = `id`
|
||||
- `Compact.serialize >> BackwardCompatible.deserialize` = `id`
|
||||
|
||||
## Microsoft Open Source Code of Conduct
|
||||
|
||||
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
This project is based on or incorporates material from the projects listed below (Third Party IP). The original copyright notice and the license under which Microsoft received such Third Party IP, are set forth below. Such licenses and notices are provided for informational purposes only. Microsoft licenses the Third Party IP to you under the licensing terms for the Microsoft product. Microsoft reserves all other rights not expressly granted under this agreement, whether by implication, estoppel or otherwise.
|
||||
|
||||
-------------------------------------------
|
||||
Newtonsoft.Json
|
||||
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2007 James Newton-King
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
Загрузка…
Ссылка в новой задаче