Skip to content

Commit

Permalink
Fix for json deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
hammett committed May 1, 2012
1 parent 836a596 commit 3ec979e
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
<OtherFlags>--keyfile:..\..\..\..\buildscripts\CastleKey.snk</OtherFlags>
</PropertyGroup>
<ItemGroup>
<Reference Include="FParsec">
<HintPath>..\..\..\..\lib\FParsec.dll</HintPath>
</Reference>
<Reference Include="FParsecCS">
<HintPath>..\..\..\..\lib\FParsecCS.dll</HintPath>
</Reference>
<Reference Include="mscorlib" />
<Reference Include="FSharp.Core" />
<Reference Include="Newtonsoft.Json">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@ module FilterParser =
(*
commonExpression = [WSP] (boolCommonExpression / methodCallExpression /
parenExpression / literalExpression / addExpression /
subExpression / mulExpression / divExpression /
modExpression / negateExpression / memberExpression
/ firstMemberExpression / castExpression) [WSP]
parenExpression / literalExpression / addExpression /
subExpression / mulExpression / divExpression /
modExpression / negateExpression / memberExpression
/ firstMemberExpression / castExpression) [WSP]
boolCommonExpression = [WSP] (boolLiteralExpression / andExpression /
orExpression /
boolPrimitiveMemberExpression / eqExpression / neExpression /
ltExpression / leExpression / gtExpression /
geExpression / notExpression / isofExpression/
boolCastExpression / boolMethodCallExpression /
firstBoolPrimitiveMemberExpression / boolParenExpression) [WSP]
orExpression /
boolPrimitiveMemberExpression / eqExpression / neExpression /
ltExpression / leExpression / gtExpression /
geExpression / notExpression / isofExpression/
boolCastExpression / boolMethodCallExpression /
firstBoolPrimitiveMemberExpression / boolParenExpression) [WSP]
parenExpression = "(" [WSP] commonExpression [WSP] ")"
boolParenExpression = "(" [WSP] boolCommonExpression [WSP] ")"
andExpression = boolCommonExpression WSP "and" WSP boolCommonExpression
Expand All @@ -79,124 +79,124 @@ isofExpression = "isof" [WSP] "("[[WSP] commonExpression [WSP] ","][WSP]stringLi
castExpression = "cast" [WSP] "("[[WSP] commonExpression [WSP] ","][WSP]stringLiteral [WSP] ")"
boolCastExpression = "cast" [WSP] "("[[WSP] commonExpression [WSP] ","][WSP] "Edm.Boolean" [WSP] ")"
firstMemberExpression = [WSP] entityNavProperty / ; section 2.2.3.1
entityComplexProperty / ; section 2.2.3.1
entitySimpleProperty ; section 2.2.3.1
entityComplexProperty / ; section 2.2.3.1
entitySimpleProperty ; section 2.2.3.1
firstBoolPrimitiveMemberExpression = entityProperty ; section 2.2.3.1
memberExpression = commonExpression [WSP] "/" [WSP]
entityNavProperty / ; section 2.2.3.1
entityComplexProperty / ; section 2.2.3.1
entitySimpleProperty ; section 2.2.3.1
entityNavProperty / ; section 2.2.3.1
entityComplexProperty / ; section 2.2.3.1
entitySimpleProperty ; section 2.2.3.1
boolPrimitiveMemberExpression = commonExpression [WSP] "/" [WSP]
entityProperty
; section 2.2.3.1
entityProperty
; section 2.2.3.1
literalExpression = stringLiteral ; section 2.2.2
/ dateTimeLiteral ; section 2.2.2
/ decimalLiteral ; section 2.2.2
/ guidUriLiteral ; section 2.2.2
/ singleLiteral ; section 2.2.2
/ doubleLiteral ; section 2.2.2
/ int16Literal ; section 2.2.2
/ int32Literal ; section 2.2.2
/ int64Literal ; section 2.2.2
/ binaryLiteral ; section 2.2.2
/ nullLiteral ; section 2.2.2
/ byteLiteral ; section 2.2.2
/ dateTimeLiteral ; section 2.2.2
/ decimalLiteral ; section 2.2.2
/ guidUriLiteral ; section 2.2.2
/ singleLiteral ; section 2.2.2
/ doubleLiteral ; section 2.2.2
/ int16Literal ; section 2.2.2
/ int32Literal ; section 2.2.2
/ int64Literal ; section 2.2.2
/ binaryLiteral ; section 2.2.2
/ nullLiteral ; section 2.2.2
/ byteLiteral ; section 2.2.2
boolLiteralExpression = boolLiteral ; section 2.2.2
methodCallExpression = boolMethodExpression
/ indexOfMethodCallExpression
/ replaceMethodCallExpression
/ toLowerMethodCallExpression
/ toUpperMethodCallExpression
/ trimMethodCallExpression
/ substringMethodCallExpression
/ concatMethodCallExpression
/ lengthMethodCallExpression
/ yearMethodCallExpression
/ monthMethodCallExpression
/ dayMethodCallExpression
/ hourMethodCallExpression
/ minuteMethodCallExpression
/ secondMethodCallExpression
/ roundMethodCallExpression
/ floorMethodCallExpression
/ ceilingMethodCallExpression
/ indexOfMethodCallExpression
/ replaceMethodCallExpression
/ toLowerMethodCallExpression
/ toUpperMethodCallExpression
/ trimMethodCallExpression
/ substringMethodCallExpression
/ concatMethodCallExpression
/ lengthMethodCallExpression
/ yearMethodCallExpression
/ monthMethodCallExpression
/ dayMethodCallExpression
/ hourMethodCallExpression
/ minuteMethodCallExpression
/ secondMethodCallExpression
/ roundMethodCallExpression
/ floorMethodCallExpression
/ ceilingMethodCallExpression
boolMethodExpression = endsWithMethodCallExpression
/ startsWithMethodCallExpression
/ substringOfMethodCallExpression
/ startsWithMethodCallExpression
/ substringOfMethodCallExpression
endsWithMethodCallExpression = "endswith" [WSP]
"(" [WSP] commonexpression [WSP]
"," [WSP] commonexpression [WSP] ")"
"(" [WSP] commonexpression [WSP]
"," [WSP] commonexpression [WSP] ")"
indexOfMethodCallExpression = "indexof" [WSP]
"(" [WSP] commonexpression [WSP]
"," [WSP] commonexpression [WSP] ")"
"(" [WSP] commonexpression [WSP]
"," [WSP] commonexpression [WSP] ")"
replaceMethodCallExpression = "replace" [WSP]
"(" [WSP] commonexpression [WSP]
"," [WSP] commonexpression [WSP]
"," [WSP] commonexpression [WSP] ")"
"(" [WSP] commonexpression [WSP]
"," [WSP] commonexpression [WSP]
"," [WSP] commonexpression [WSP] ")"
startsWithMethodCallExpression = "startswith" [WSP]
"(" [WSP] commonexpression [WSP]
"," [WSP] commonexpression [WSP] ")"
"(" [WSP] commonexpression [WSP]
"," [WSP] commonexpression [WSP] ")"
toLowerMethodCallExpression = "tolower" [WSP]
"(" [WSP] commonexpression [WSP] ")"
"(" [WSP] commonexpression [WSP] ")"
toUpperMethodCallExpression = "toupper" [WSP]
"(" [WSP] commonexpression [WSP] ")"
"(" [WSP] commonexpression [WSP] ")"
trimMethodCallExpression = "trim" [WSP]
"(" [WSP] commonexpression [WSP] ")"
"(" [WSP] commonexpression [WSP] ")"
substringMethodCallExpression = "substring" [WSP]
"(" [WSP] commonexpression [WSP]
[ "," [WSP] commonexpression [WSP] ] ")"
"(" [WSP] commonexpression [WSP]
[ "," [WSP] commonexpression [WSP] ] ")"
substringOfMethodCallExpression = "substringof" [WSP]
"(" [WSP] commonexpression [WSP]
[ "," [WSP] commonexpression [WSP] ] ")"
"(" [WSP] commonexpression [WSP]
[ "," [WSP] commonexpression [WSP] ] ")"
concatMethodCallExpression = "concat" [WSP]
"(" [WSP] commonexpression [WSP]
[ "," [WSP] commonexpression [WSP] ] ")"
"(" [WSP] commonexpression [WSP]
[ "," [WSP] commonexpression [WSP] ] ")"
lengthMethodCallExpression = "length" [WSP]
"(" [WSP] commonexpression [WSP] ")"
"(" [WSP] commonexpression [WSP] ")"
yearMethodCallExpression = "year" [WSP]
"(" [WSP] commonexpression [WSP] ")"
"(" [WSP] commonexpression [WSP] ")"
monthMethodCallExpression = "month" [WSP]
"(" [WSP] commonexpression [WSP] ")"
"(" [WSP] commonexpression [WSP] ")"
dayMethodCallExpression = "day" [WSP]
"(" [WSP] commonexpression [WSP] ")"
"(" [WSP] commonexpression [WSP] ")"
hourMethodCallExpression = "hour" [WSP]
"(" [WSP] commonexpression [WSP] ")"
"(" [WSP] commonexpression [WSP] ")"
minuteMethodCallExpression = "minute" [WSP]
"(" [WSP] commonexpression [WSP] ")"
"(" [WSP] commonexpression [WSP] ")"
secondMethodCallExpression = "second" [WSP]
"(" [WSP] commonexpression [WSP] ")"
"(" [WSP] commonexpression [WSP] ")"
roundMethodCallExpression = "round" [WSP]
"(" [WSP] commonexpression [WSP] ")"
"(" [WSP] commonexpression [WSP] ")"
floorMethodCallExpression = "floor" [WSP]
"(" [WSP] commonexpression [WSP] ")"
"(" [WSP] commonexpression [WSP] ")"
ceilingMethodCallExpression = "ceiling" [WSP]
"(" [WSP] commonexpression [WSP] ")"
"(" [WSP] commonexpression [WSP] ")"
*)
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ module SegmentParser =
odata |> List.iter (fun i -> odataparms.[i] <- qs.[i])
odataparms, ordinary

let filter = MetaQuerySegment.Filter "Name eq 'test'" // odataParams.["$filter"]
//let filter = MetaQuerySegment.Filter "Name eq 'test'" // odataParams.["$filter"]

let lastCollAccessSegment : Ref<UriSegment> = ref UriSegment.Nothing

Expand Down Expand Up @@ -295,7 +295,8 @@ module SegmentParser =

match !lastCollAccessSegment with
| UriSegment.EntitySet d ->
d.Filter <- null
// d.Filter <- null
()
| UriSegment.PropertyAccessCollection d -> () // lastCollAccessSegment := newSegment
| _ -> ()
()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,16 @@ module JSonSerialization =
if jsonReader.TokenType = JsonToken.PropertyName then
match rt.Properties |> Seq.tryFind (fun p -> p.Name = jsonReader.Value.ToString()) with
| Some prop ->
jsonReader.Read() |> ignore
// todo: assert is not comment or property name

let value = jsonReader.Value

if prop.IsOfKind (ResourcePropertyKind.Primitive) then
let value = jsonReader.Value
prop.SetValue(instance, value)

let sanitizedVal = Convert.ChangeType(value, prop.ResourceType.InstanceType)

prop.SetValue(instance, sanitizedVal)

elif prop.IsOfKind (ResourcePropertyKind.ComplexType) then

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
<HintPath>..\..\..\..\tests\lib\FluentAssertions.dll</HintPath>
</Reference>
<Reference Include="FSharp.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="Newtonsoft.Json, Version=4.0.5.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\..\..\..\lib\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="nunit.framework">
<HintPath>..\..\..\..\tests\lib\nunit.framework.dll</HintPath>
</Reference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,34 @@ public void EntitySet_Create_Atom_Atom_Success()
deserializedProd.Price.Should().Be(prod.Price);
}

[Test, Description("Id for products needs to refer back to EntityContainer.Products")]
public void EntitySet_Create_Json_Json_Success()
{
var prod = new Product1()
{
Created = DateTime.Now,
Modified = DateTime.Now,
IsCurated = true,
Name = "testing",
Price = 2.3m
};

Process("/Products/", SegmentOp.Create, _model,
accept: MediaTypes.JSon, contentType: MediaTypes.JSon,
inputStream: new MemoryStream(Encoding.UTF8.GetBytes(prod.ToJSon())));

Assertion.ResponseIs(201, MediaTypes.JSon);

Assertion.Callbacks.CreateWasCalled(1);

var deserializedProd = (Product1)_created.ElementAt(0).Item3;

deserializedProd.Name.Should().Be(prod.Name);
deserializedProd.IsCurated.Should().Be(prod.IsCurated);
deserializedProd.Modified.Should().BeWithin(TimeSpan.FromSeconds(1.0)).After(prod.Modified);
deserializedProd.Created.Should().BeWithin(TimeSpan.FromSeconds(1.0)).After(prod.Created);
deserializedProd.Price.Should().Be(prod.Price);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Text;
using System.Xml;
using FluentAssertions;
using Newtonsoft.Json;
using NUnit.Framework;

static class SyndicationExtensions
Expand Down Expand Up @@ -343,6 +344,13 @@ public SyndicationItem ToSyndicationItem()
content.Add("IsCurated", "Edm.Boolean", XmlConvert.ToString(this.IsCurated));
return item;
}

public string ToJSon()
{
var writer = new StringWriter();
Newtonsoft.Json.JsonSerializer.Create(new JsonSerializerSettings()).Serialize(writer, this);
return writer.GetStringBuilder().ToString();
}
}
}
}

0 comments on commit 3ec979e

Please sign in to comment.