PR 717: Expose serializer as module instead of static type

- Update Nuget package name
 - Expose serializer as module instead of static type
This commit is contained in:
William Blum 2016-05-05 02:26:14 +00:00
Родитель b4fbebb15e
Коммит bd9251acba
10 изменённых файлов: 118 добавлений и 39 удалений

1
.gitignore поставляемый
Просмотреть файл

@ -124,3 +124,4 @@ ModelManifest.xml
.nuget/NuGet.exe
.vs/*
*FSharpLu.*.nupkg
.nuget/CredentialProvider.VSS.exe

Просмотреть файл

@ -184,12 +184,41 @@ type CompactUnionJsonConverter() =
else
failwithf "Unexpected Json token type %O: %O" jToken.Type jToken
/// Serialization settings for our custom compact Json converter
type CompactSettings =
static member settings =
let s = JsonSerializerSettings(NullValueHandling = NullValueHandling.Ignore)
s.Converters.Add(CompactUnionJsonConverter())
s
/// Default NetwonSoft Json.Net serializer
module Compact =
open System.Runtime.CompilerServices
/// Our Json serializer
type Compact = With<CompactSettings>
module Internal =
/// Serialization settings for our compact Json converter
type Settings =
static member settings =
let s = JsonSerializerSettings(NullValueHandling = NullValueHandling.Ignore)
s.Converters.Add(CompactUnionJsonConverter())
s
type private S = With<Internal.Settings>
/// Serialize an object to Json with the specified converter
[<MethodImplAttribute(MethodImplOptions.NoInlining)>]
let inline serialize< ^T> x = S.serialize x
/// Serialize an object to Json with the specified converter and save the result to a file
[<MethodImplAttribute(MethodImplOptions.NoInlining)>]
let inline serializeToFile< ^T> file obj = S.serializeToFile file obj
/// Try to deserialize json to an object of type ^T
[<MethodImplAttribute(MethodImplOptions.NoInlining)>]
let inline tryDeserialize< ^T> json = S.tryDeserialize< ^T> json
/// Try to read Json from a file and desrialized it to an object of type ^T
[<MethodImplAttribute(MethodImplOptions.NoInlining)>]
let inline tryDeserializeFile< ^T> file = S.tryDeserializeFile< ^T> file
/// Try to deserialize a stream to an object of type ^T
[<MethodImplAttribute(MethodImplOptions.NoInlining)>]
let inline tryDeserializeStream< ^T> stream = S.tryDeserializeStream< ^T> stream
/// Deserialize a Json to an object of type ^T
[<MethodImplAttribute(MethodImplOptions.NoInlining)>]
let inline deserialize< ^T> json : ^T = S.deserialize< ^T> json
/// Read Json from a file and desrialized it to an object of type ^T
[<MethodImplAttribute(MethodImplOptions.NoInlining)>]
let inline deserializeFile< ^T> file = S.deserializeFile< ^T> file
/// Deserialize a stream to an object of type ^T
[<MethodImplAttribute(MethodImplOptions.NoInlining)>]
let inline deserializeStream< ^T> stream = S.deserializeStream< ^T> stream

Просмотреть файл

@ -8,7 +8,7 @@
<ProjectGuid>81eaf46a-bf20-4871-bf8e-c94fd478e9e8</ProjectGuid>
<OutputType>Library</OutputType>
<RootNamespace>FSharpLu.Json</RootNamespace>
<AssemblyName>FSharpLu.Json</AssemblyName>
<AssemblyName>Microsoft.FSharpLu.Json</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<TargetFSharpCoreVersion>4.4.0.0</TargetFSharpCoreVersion>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
@ -23,7 +23,7 @@
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<WarningLevel>3</WarningLevel>
<DocumentationFile>bin\Debug\FSharpLu.Json.XML</DocumentationFile>
<DocumentationFile>bin\Debug\Microsoft.FSharpLu.Json.XML</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
@ -32,7 +32,7 @@
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<WarningLevel>3</WarningLevel>
<DocumentationFile>bin\Release\FSharpLu.Json.XML</DocumentationFile>
<DocumentationFile>bin\Release\Microsoft.FSharpLu.Json.XML</DocumentationFile>
</PropertyGroup>
<PropertyGroup>
<MinimumVisualStudioVersion Condition="'$(MinimumVisualStudioVersion)' == ''">11</MinimumVisualStudioVersion>

Просмотреть файл

@ -1,7 +1,7 @@
<?xml version="1.0"?>
<package >
<metadata>
<id>$id$</id>
<id>Microsoft.FSharpLu.Json</id>
<version>$version$</version>
<title>$title$</title>
<authors>william.blum@microsoft.com</authors>

Просмотреть файл

@ -39,14 +39,14 @@ let inline tryDeserializeWithBoth< ^T , ^V when ^T :equality>
(rewind : unit -> unit)
(v:^V)
=
match tryCatchJsonSerializationException true firstDeserialize v with
| Choice1Of2 _ as parsedObject -> parsedObject
| Choice1Of2 _ as parsedObject ->
parsedObject
| Choice2Of2 firstException ->
rewind()
match tryCatchJsonSerializationException true secondDeserialize v with
| Choice1Of2 _ as parsedObject -> parsedObject
| Choice2Of2 secondException ->
Choice2Of2 <| (JsonSerializationException("Could not deserialize with any of the serializers. See inner exception for error reported by the first deserializer.", firstException) :> System.Exception)
rewind()
match tryCatchJsonSerializationException true secondDeserialize v with
| Choice1Of2 _ as parsedObject -> parsedObject
| Choice2Of2 secondException ->
Choice2Of2 <| (JsonSerializationException("Could not deserialize with any of the serializers. See inner exception for error reported by the first deserializer.", firstException) :> System.Exception)

Просмотреть файл

@ -1,13 +1,51 @@
namespace Microsoft.FSharpLu.Json
open Newtonsoft.Json
/// Default serialization settings with default NetwonSoft Json.Net's serialization
type DefaultSettings =
static member settings =
let s = JsonSerializerSettings(NullValueHandling = NullValueHandling.Ignore)
s.Converters.Add(Converters.StringEnumConverter())
s
/// Default NetwonSoft Json.Net serializer
type Default = With<DefaultSettings>
module Default =
open Newtonsoft.Json
open System.Runtime.CompilerServices
/// We don't want to expose type JsonSerializerSettings in the current namespace as it would
/// force any calling assembly to unecessarily add a direct reference to Newtosoft.Json.
/// Instead we nest it under an Internal module.
/// (Cannot actually be marked 'internal' or 'private' due to restriction in F#
/// with static type inlining.
module Internal =
/// Serialization settings for the default NetwonSoft Json.Net's serialization format
type DefaultSettings =
static member settings =
let s = JsonSerializerSettings(NullValueHandling = NullValueHandling.Ignore)
s.Converters.Add(Converters.StringEnumConverter())
s
type private S = With<Internal.DefaultSettings>
// A side-effect of above comment is that it forces us to manually inline the type alias
// and redefine all the method below manually. We also need to annotate the wrappers
// with the NoInlining attribute to prevent the code to be inlined in caller assemblies.
/// Serialize an object to Json with the specified converter
[<MethodImplAttribute(MethodImplOptions.NoInlining)>]
let inline serialize< ^T> x = S.serialize x
/// Serialize an object to Json with the specified converter and save the result to a file
[<MethodImplAttribute(MethodImplOptions.NoInlining)>]
let inline serializeToFile< ^T> file obj = S.serializeToFile file obj
/// Try to deserialize json to an object of type ^T
[<MethodImplAttribute(MethodImplOptions.NoInlining)>]
let inline tryDeserialize< ^T> json = S.tryDeserialize< ^T> json
/// Try to read Json from a file and desrialized it to an object of type ^T
[<MethodImplAttribute(MethodImplOptions.NoInlining)>]
let inline tryDeserializeFile< ^T> file = S.tryDeserializeFile< ^T> file
/// Try to deserialize a stream to an object of type ^T
[<MethodImplAttribute(MethodImplOptions.NoInlining)>]
let inline tryDeserializeStream< ^T> stream = S.tryDeserializeStream< ^T> stream
/// Deserialize a Json to an object of type ^T
[<MethodImplAttribute(MethodImplOptions.NoInlining)>]
let inline deserialize< ^T> json : ^T = S.deserialize< ^T> json
/// Read Json from a file and desrialized it to an object of type ^T
[<MethodImplAttribute(MethodImplOptions.NoInlining)>]
let inline deserializeFile< ^T> file = S.deserializeFile< ^T> file
/// Deserialize a stream to an object of type ^T
[<MethodImplAttribute(MethodImplOptions.NoInlining)>]
let inline deserializeStream< ^T> stream = S.deserializeStream< ^T> stream

Просмотреть файл

@ -1,8 +1,12 @@
namespace Microsoft.FSharpLu.Json
open Newtonsoft.Json
open System.Runtime.CompilerServices
/// Functor used to create Json serialization helpers for specific serializer settings
/// Warning: Because this functor depends on type JsonSerializerSettings defined in
/// NewtonSoft.Json any calling assembly using this type will
/// also need to add a direct reference to NewtonSoft.Json.
type With< ^S when ^S : (static member settings : JsonSerializerSettings) > =
/// Serialize an object to Json with the specified converter

Просмотреть файл

@ -8,7 +8,7 @@
<ProjectGuid>1a22180f-57c3-4415-8d1a-4ad523967251</ProjectGuid>
<OutputType>Library</OutputType>
<RootNamespace>FSharpLu.Tests</RootNamespace>
<AssemblyName>FSharpLu.Tests</AssemblyName>
<AssemblyName>Microsoft.FSharpLu.Tests</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<TargetFSharpCoreVersion>4.4.0.0</TargetFSharpCoreVersion>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>

Просмотреть файл

@ -19,8 +19,8 @@ type 'a NestedOptions = 'a option option option option
type 'a Ambiguous = { Some : 'a }
let inline serialize< ^T> x = Compact.serialize< ^T> x
let inline deserialize x = Compact.deserialize x
let inline serialize< ^T> (x: ^T) = Compact.serialize< ^T> x
let inline deserialize< ^T> x : ^T = Compact.deserialize< ^T> x
let inline reciprocal< ^T when ^T:equality> (x: ^T) =
// theoretically one round trip is sufficient; we perform
@ -28,8 +28,8 @@ let inline reciprocal< ^T when ^T:equality> (x: ^T) =
// in the serialization functions
x |> serialize |> deserialize |> serialize |> deserialize = x
let inline areReciprocal (x:'T) =
Assert.IsTrue(reciprocal x)
let inline areReciprocal< ^T when ^T:equality> (x: ^T) =
Assert.IsTrue(reciprocal< ^T> x)
/// Check that given object serializes to the specified Json string
let inline serializedAs json o =

Просмотреть файл

@ -1,9 +1,9 @@
# Pack projects as Nuget packages
param(
$feed,
$key,
$key = 'Springfield',
[switch]$skipBuild,
[switch]$push
[switch]$push
)
$root = Split-Path -parent $psScriptRoot
@ -19,8 +19,15 @@ if(-not $skipBuild) {
& $nuget pack $root\FSharpLu\FSharpLu.fsproj -Prop Configuration=Release -Prop VisualStudioVersion=14.0
& $nuget pack $root\FSharpLu.Json\FSharpLu.Json.fsproj -Prop Configuration=Release -Prop VisualStudioVersion=14.0
function pushLatest($name) {
$file = [string](gci "$name.*.*.*.*.nupkg" | sort -Descending -Property LastAccessTime | select -First 1)
Write-Host "Pushing $file"
& $nuget push -source $feed $file -ApiKey $key
}
if ($push) {
& $nuget push -source $feed *.nupkg -ApiKey $key
pushLatest 'Microsoft.FSharpLu'
pushLatest 'Microsoft.FSharpLu.Json'
}
popd