Skip to content

Commit

Permalink
Added Castle.MonoRail.Extension.OData.Serialization namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
hammett committed May 10, 2012
1 parent 8e70725 commit 687c03d
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 34 deletions.
Expand Up @@ -29,6 +29,7 @@ open System.Text
open System.Xml
open System.Xml.Linq
open Castle.MonoRail
open Castle.MonoRail.Extension.OData.Serialization

// http://msdn.microsoft.com/en-us/library/dd233205.aspx

Expand Down Expand Up @@ -107,8 +108,8 @@ module SegmentProcessor =
| _ -> ()


let internal serialize_result (reply:ResponseToSend) (request:RequestParameters) (response:ResponseParameters) (containerUri:Uri) =
let s = SerializerFactory.Create(response.contentType)
let internal serialize_result (reply:ResponseToSend) (request:RequestParameters) (response:ResponseParameters) (containerUri:Uri) useSpecialJson =
let s = SerializerFactory.Create(response.contentType, useSpecialJson)
let wrapper = request.wrapper

s.Serialize(reply, wrapper, request.baseUri, containerUri, response.writer, response.contentEncoding)
Expand Down Expand Up @@ -466,10 +467,8 @@ module SegmentProcessor =
callbacks.Operation(actionOp.ResourceType, parameters, actionOp.Name)
// it's understood that the action took care of the result
emptyResponse
// shouldContinue := false

| UriSegment.RootServiceOperation ->
emptyResponse
| UriSegment.RootServiceOperation -> emptyResponse

| UriSegment.EntitySet d ->
process_entityset op d previous hasMoreSegments model callbacks shouldContinue request response parameters
Expand Down Expand Up @@ -505,6 +504,8 @@ module SegmentProcessor =
process_operation_value lastSegment navResult response
| _ -> failwithf "Unsupported meta instruction %O" meta

let useSpecialJson = ref false

for metaQuery in metaQueries do
match metaQuery with
| MetaQuerySegment.Filter exp ->
Expand All @@ -517,10 +518,10 @@ module SegmentProcessor =
()
| MetaQuerySegment.Format fmt ->
match fmt.ToLowerInvariant() with
| "json" -> response.contentType <- MediaTypes.JSon
| "xml" -> response.contentType <- MediaTypes.Xml
| "atom" -> response.contentType <- MediaTypes.Atom
// | "simplejson" -> response.contentType <-
| "json" -> response.contentType <- MediaTypes.JSon
| "xml" -> response.contentType <- MediaTypes.Xml
| "atom" -> response.contentType <- MediaTypes.Atom
| "simplejson" -> response.contentType <- MediaTypes.JSon; useSpecialJson := true
| _ -> failwithf "Unsupported format value %O" fmt
| MetaQuerySegment.Select exp ->
()
Expand All @@ -534,7 +535,7 @@ module SegmentProcessor =
if result <> emptyResponse then
if response.contentType = null then
response.contentType <- callbacks.negotiateContent.Invoke( result.SingleResult <> null ) // segments request.accept
serialize_result result request response result.FinalResourceUri
serialize_result result request response result.FinalResourceUri !useSpecialJson

end

Expand Up @@ -13,7 +13,7 @@
// Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
// 02110-1301 USA, or see the FSF site: http://www.fsf.org.

namespace Castle.MonoRail.Extension.OData
namespace Castle.MonoRail.Extension.OData.Serialization

open System
open System.Collections
Expand Down Expand Up @@ -165,7 +165,6 @@ module AtomSerialization =
| Some rs ->
// for this case, we always want to append the key
Uri(svcBaseUri, rs.Name + rt.GetKey(instance))

| _ ->
System.Diagnostics.Debug.Assert (containerUri <> null)
if appendKey
Expand Down Expand Up @@ -308,8 +307,6 @@ module AtomSerialization =

instance



let CreateDeserializer () =
{ new Deserializer() with
override x.DeserializeMany (rt, reader, enc) =
Expand Down
Expand Up @@ -13,21 +13,24 @@
// Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
// 02110-1301 USA, or see the FSF site: http://www.fsf.org.

namespace Castle.MonoRail.Extension.OData
namespace Castle.MonoRail.Extension.OData.Serialization

open System
open System.Xml


type SerializerFactory() =

static member Create(contentType:string) : Serializer =
static member Create(contentType:string, useSpecialJson:bool) : Serializer =

match contentType.ToLowerInvariant() with
| "application/atom+xml" ->
AtomSerialization.CreateSerializer()

| "application/json" ->
// if useSpecialJson
// then JSonSimpleSerialization.CreateSerializer()
// else
JSonSerialization.CreateSerializer()

| "text/xml"
Expand Down
Expand Up @@ -13,7 +13,7 @@
// Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
// 02110-1301 USA, or see the FSF site: http://www.fsf.org.

namespace Castle.MonoRail.Extension.OData
namespace Castle.MonoRail.Extension.OData.Serialization

open System
open System.Collections
Expand All @@ -29,6 +29,15 @@ open System.Data.Services.Common
open Newtonsoft.Json





module JSonSimpleSerialization =
begin

end


module JSonSerialization =
begin
let private set_up (writer:JsonTextWriter) =
Expand All @@ -47,7 +56,6 @@ module JSonSerialization =
writer.WriteValue rt.FullName
writer.WriteEndObject()


let rec private write_primitive_and_complex_properties (writer:JsonTextWriter) (instance) (uri:Uri) (rt:ResourceType) =
for prop in rt.Properties do
let otherRt = prop.ResourceType
Expand Down Expand Up @@ -162,12 +170,7 @@ module JSonSerialization =
jsonWriter.WritePropertyName "d"

write_set jsonWriter wrapper svcBaseUri containerUri rt items true propertiesToExpand
(*
jsonWriter.WriteStartArray()
for item in items do
write_js_item jsonWriter wrapper item svcBaseUri containerUri rt true
jsonWriter.WriteEndArray()
*)

jsonWriter.WriteEndObject()


Expand Down Expand Up @@ -201,6 +204,9 @@ module JSonSerialization =
jsonWriter.WriteEndObject() // d
jsonWriter.WriteEndObject()




let internal read_item (rt:ResourceType) (reader:TextReader) (enc:Encoding) =

use jsonReader = new JsonTextReader(reader)
Expand Down Expand Up @@ -239,8 +245,6 @@ module JSonSerialization =
else
()



doContinue := jsonReader.Read()

| _ ->
Expand All @@ -254,15 +258,17 @@ module JSonSerialization =


let CreateDeserializer () =
{ new Deserializer() with
{
new Deserializer() with
override x.DeserializeMany (rt, reader, enc) =
raise(NotImplementedException())
override x.DeserializeSingle (rt, reader, enc) =
read_item rt reader enc
}

let CreateSerializer () =
{ new Serializer() with
{
new Serializer() with
override x.SerializeMany(wrapper, svcBaseUri, containerUri , rt, items, writer, enc, propertiesToExpand) =
write_items wrapper svcBaseUri containerUri rt items writer enc propertiesToExpand
override x.SerializeSingle(wrapper, svcBaseUri, containerUri, rt, item, writer, enc, propertiesToExpand) =
Expand Down
Expand Up @@ -13,7 +13,7 @@
// Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
// 02110-1301 USA, or see the FSF site: http://www.fsf.org.

namespace Castle.MonoRail.Extension.OData
namespace Castle.MonoRail.Extension.OData.Serialization

open System
open System.Collections.Generic
Expand Down
Expand Up @@ -13,7 +13,7 @@
// Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
// 02110-1301 USA, or see the FSF site: http://www.fsf.org.

namespace Castle.MonoRail.Extension.OData
namespace Castle.MonoRail.Extension.OData.Serialization

open System
open System.IO
Expand Down
Expand Up @@ -13,7 +13,7 @@
// Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
// 02110-1301 USA, or see the FSF site: http://www.fsf.org.

namespace Castle.MonoRail.Extension.OData
namespace Castle.MonoRail.Extension.OData.Serialization

open System
open System.Xml
Expand Down
Expand Up @@ -13,7 +13,7 @@
// Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
// 02110-1301 USA, or see the FSF site: http://www.fsf.org.

namespace Castle.MonoRail.Extension.OData
namespace Castle.MonoRail.Extension.OData.Serialization

open System
open System.Collections
Expand All @@ -27,6 +27,7 @@ open Castle.MonoRail
open System.Data.OData
open System.Data.Services.Providers


type ResponseToSend = {
mutable QItems : IQueryable;
mutable SingleResult : obj;
Expand All @@ -43,6 +44,33 @@ type Deserializer() =
abstract member DeserializeSingle : rt:ResourceType * reader:TextReader * enc:Encoding -> obj
end

[<AbstractClass;AllowNullLiteral>]
type SerializerStructure() =
class
member x.ShouldExpand (property:ResourceProperty) =
false

member x.WriteItems () =
// ProcessSkipTop ?
// ProcessSkipToken ?
()

member x.WriteItem () =
()

member x.WriteProperty () =
()

member x.InternalWriteItems () =
()

member x.InternalWriteItem () =
()

member x.InternalProperty () =
()

end

[<AbstractClass;AllowNullLiteral>]
type Serializer() =
Expand Down Expand Up @@ -87,7 +115,7 @@ module SerializerCommons =
if !doCont then doCont := x.Read()
!isElement

type System.Data.Services.Providers.ResourceProperty
type ResourceProperty
with
member x.GetValue(instance:obj) =
let prop = instance.GetType().GetProperty(x.Name)
Expand All @@ -102,7 +130,7 @@ module SerializerCommons =
let prop = instance.GetType().GetProperty(x.Name)
prop.SetValue(instance, value, null)

type System.Data.Services.Providers.ResourceType
type ResourceType
with
member x.GetKey (instance:obj) =
let keyValue =
Expand Down
Expand Up @@ -9,6 +9,7 @@
using System.Linq;
using System.Text;
using NUnit.Framework;
using Castle.MonoRail.Extension.OData.Serialization;

[TestFixture]
public class AtomServiceDocSerializerTestCase
Expand Down
Expand Up @@ -12,6 +12,7 @@
using System.Xml.Schema;
using FluentAssertions;
using NUnit.Framework;
using Castle.MonoRail.Extension.OData.Serialization;

[TestFixture]
public class MetadataSerializerTestCase
Expand Down
Expand Up @@ -14,6 +14,8 @@
using FluentAssertions;
using Newtonsoft.Json;
using NUnit.Framework;
using Castle.MonoRail.Extension.OData.Serialization;


static class SyndicationExtensions
{
Expand Down
Binary file modified MR3/Extensions/OData/tests/test odata stuff.xlsx
Binary file not shown.

0 comments on commit 687c03d

Please sign in to comment.