Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added lib/tests/FSharp.Core.dll
Binary file not shown.
16 changes: 11 additions & 5 deletions src/ServiceStack.Text.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServiceStack.Text", "Servic
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServiceStack.Text.Tests", "..\tests\ServiceStack.Text.Tests\ServiceStack.Text.Tests.csproj", "{9770BD40-AA3B-4785-B5E0-F4C470F9F14E}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "ServiceStack.Text.FSharp.Tests", "..\tests\ServiceStack.Text.FSharp.Tests\ServiceStack.Text.FSharp.Tests.fsproj", "{1F03048A-D5B2-4950-9FAD-632A5DE78C94}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
MonoTouch|Any CPU = MonoTouch|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{579B3FDB-CDAD-44E1-8417-885C38E49A0E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
Expand All @@ -29,8 +31,15 @@ Global
{9770BD40-AA3B-4785-B5E0-F4C470F9F14E}.MonoTouch|Any CPU.ActiveCfg = MonoTouch|Any CPU
{9770BD40-AA3B-4785-B5E0-F4C470F9F14E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9770BD40-AA3B-4785-B5E0-F4C470F9F14E}.Release|Any CPU.Build.0 = Release|Any CPU
{1F03048A-D5B2-4950-9FAD-632A5DE78C94}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1F03048A-D5B2-4950-9FAD-632A5DE78C94}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1F03048A-D5B2-4950-9FAD-632A5DE78C94}.MonoTouch|Any CPU.ActiveCfg = Release|Any CPU
{1F03048A-D5B2-4950-9FAD-632A5DE78C94}.MonoTouch|Any CPU.Build.0 = Release|Any CPU
{1F03048A-D5B2-4950-9FAD-632A5DE78C94}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1F03048A-D5B2-4950-9FAD-632A5DE78C94}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
StartupItem = ServiceStack.Text\ServiceStack.Text.csproj
Expand All @@ -42,7 +51,4 @@ Global
$2.Text =
$2.IncludeInNewFiles = True
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Binary file modified src/ServiceStack.Text.suo
Binary file not shown.
46 changes: 35 additions & 11 deletions src/ServiceStack.Text/Common/DeserializeType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static ParseStringDelegate GetParseMethod(Type type)
}

private static object StringToType(Type type, string strType,
EmptyCtorDelegate ctorFn,
EmptyCtorDelegate ctorFn,
IDictionary<string, SetPropertyDelegate> setterMap,
IDictionary<string, ParseStringDelegate> parseStringFnMap)
{
Expand Down Expand Up @@ -78,15 +78,15 @@ private static object StringToType(Type type, string strType,

var propertyValueString = Serializer.EatValue(strType, ref index);

if (!parseStringFnMap.TryGetValue(propertyName, out parseStringFn))
{
// try changing case of the first character
var p2 = propertyName.ToggleFirstChar();
if (p2 != null && parseStringFnMap.TryGetValue(p2, out parseStringFn))
{
propertyName = p2;
}
}
if (!parseStringFnMap.TryGetValue(propertyName, out parseStringFn))
{
// try changing case of the first character
var p2 = propertyName.ToggleFirstChar();
if (p2 != null && parseStringFnMap.TryGetValue(p2, out parseStringFn))
{
propertyName = p2;
}
}

if (parseStringFn != null)
{
Expand All @@ -106,10 +106,34 @@ private static object StringToType(Type type, string strType,
return instance;
}

private static SetPropertyDelegate FSharpRecordSetPropertyPolicy(PropertyInfo property)
{
string propertyName = property.Name;
string fieldName = propertyName.EndsWith("@") ? propertyName : propertyName + "@";
FieldInfo fi = property.DeclaringType.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
if (fi == null) return null;
return GetSetFieldMethod(property.DeclaringType, fi);
}

public static SetPropertyDelegate GetSetFieldMethod(Type type, FieldInfo fieldInfo)
{
var oInstanceParam = Expression.Parameter(typeof(object), "oInstanceParam");
var oValueParam = Expression.Parameter(typeof(object), "oValueParam");
var instanceParam = Expression.Convert(oInstanceParam, type);
var useType = fieldInfo.FieldType;
var valueParam = Expression.Convert(oValueParam, useType);
return Expression.Lambda<SetPropertyDelegate>(
Expression.Assign(Expression.Field(instanceParam, fieldInfo), valueParam),
oInstanceParam,
oValueParam)
.Compile();
}

public static SetPropertyDelegate GetSetPropertyMethod(Type type, PropertyInfo propertyInfo)
{
var setMethodInfo = propertyInfo.GetSetMethod(true);
if (setMethodInfo == null) return null;
// if there's no setter, let's see if we can try the F# policy of a private field suffixed by @.
if (setMethodInfo == null) return FSharpRecordSetPropertyPolicy(propertyInfo);

#if SILVERLIGHT || MONOTOUCH
return (instance, value) => setMethodInfo.Invoke(instance, new[] {value});
Expand Down
3 changes: 2 additions & 1 deletion src/ServiceStack.Text/ServiceStack.Text.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ServiceStack.Text</RootNamespace>
<AssemblyName>ServiceStack.Text</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<FileUpgradeFlags>
</FileUpgradeFlags>
Expand All @@ -31,6 +31,7 @@
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down
6 changes: 6 additions & 0 deletions tests/ServiceStack.Text.FSharp.Tests/Records.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace ServiceStack.Text.FSharp.Tests

type Coordinate = {X:double; Y:double}

type Line = {Start:Coordinate; End:Coordinate}

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{1f03048a-d5b2-4950-9fad-632a5de78c94}</ProjectGuid>
<OutputType>Library</OutputType>
<RootNamespace>ServiceStack.Text.FSharp.Tests</RootNamespace>
<AssemblyName>ServiceStack.Text.FSharp.Tests</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<Name>ServiceStack.Text.FSharp.Tests</Name>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<Tailcalls>false</Tailcalls>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<WarningLevel>3</WarningLevel>
<DocumentationFile>
</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<Tailcalls>true</Tailcalls>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<WarningLevel>3</WarningLevel>
<DocumentationFile>
</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="mscorlib" />
<Reference Include="FSharp.Core" />
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
<Compile Include="Records.fs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\FSharp\1.0\Microsoft.FSharp.Targets" Condition="!Exists('$(MSBuildBinPath)\Microsoft.Build.Tasks.v4.0.dll')" />
<Import Project="$(MSBuildExtensionsPath32)\..\Microsoft F#\v4.0\Microsoft.FSharp.Targets" Condition=" Exists('$(MSBuildBinPath)\Microsoft.Build.Tasks.v4.0.dll')" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
18 changes: 18 additions & 0 deletions tests/ServiceStack.Text.Tests/JsonTests/BasicJsonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,24 @@ public IntIntDictionary()
public IDictionary<int,int> Dictionary { get; set; }
}

[Test]
public void Can_deserialize_simple_fsharp_record()
{
var s = JsonSerializer.DeserializeFromString<ServiceStack.Text.FSharp.Tests.Coordinate>("{\"X\":1.0,\"Y\":2.0}");
Assert.AreEqual(1.0, s.X);
Assert.AreEqual(2.0, s.Y);
}

[Test]
public void Can_deserialize_nested_fsharp_record()
{
var s = JsonSerializer.DeserializeFromString<ServiceStack.Text.FSharp.Tests.Line>("{\"Start\":{\"X\":1.0,\"Y\":2.0},\"End\":{\"X\":3.0,\"Y\":4.0}}");
Assert.AreEqual(1.0, s.Start.X);
Assert.AreEqual(2.0, s.Start.Y);
Assert.AreEqual(3.0, s.End.X);
Assert.AreEqual(4.0, s.End.Y);
}

[Test]
public void Serialize_skips_null_values_by_default()
{
Expand Down
10 changes: 9 additions & 1 deletion tests/ServiceStack.Text.Tests/ServiceStack.Text.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ServiceStack.Text.Tests</RootNamespace>
<AssemblyName>ServiceStack.Text.Tests</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<FileUpgradeFlags>
</FileUpgradeFlags>
Expand All @@ -31,6 +31,7 @@
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -61,6 +62,9 @@
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="FSharp.Core, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\lib\tests\FSharp.Core.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
Expand Down Expand Up @@ -206,6 +210,10 @@
<Project>{579B3FDB-CDAD-44E1-8417-885C38E49A0E}</Project>
<Name>ServiceStack.Text</Name>
</ProjectReference>
<ProjectReference Include="..\ServiceStack.Text.FSharp.Tests\ServiceStack.Text.FSharp.Tests.fsproj">
<Project>{1F03048A-D5B2-4950-9FAD-632A5DE78C94}</Project>
<Name>ServiceStack.Text.FSharp.Tests</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down