datacontractserializer

How can I make DataContractJsonSerializer serialize an object as a string?

谁说我不能喝 提交于 2019-11-30 18:31:52
I have a struct in C# that wraps a guid. I'm using DataContractJsonSerializer to serialize an object containing an instance of that class. When I was using a guid directly, it was serialized as a plain string, but now it's serialized as a name/value pair. Here's an NUnit test and supporting code that demonstrates the problem: private static string ToJson<T>(T data) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof (T)); using (MemoryStream ms = new MemoryStream()) { serializer.WriteObject(ms, data); return Encoding.Default.GetString(ms.ToArray()); } }

Configuring WCF client and service for use with protobuf-net

亡梦爱人 提交于 2019-11-30 16:54:57
I decided to open a new question about this matter, maybe expanding this question, not having found a precise answer about the issue anywhere on the Internet. I want to use protobuf-net to serialize/deserialize messages exchanged between my WCF client and service. The service is self-hosted in a Windows Service. Both client and service are configured programmatically, using a custom binding very similar to wsHttpBinding . Service reference code is generated using "Add Service Reference" option in Visual Studio. The ORM used on the WCF service is EntityFramework 4 and it's code is generated

XML Serialisation - When To Use DataContractSerializer / Binary / XMLSerialiser

南楼画角 提交于 2019-11-30 09:55:04
I ve been looking at this for a while now It seems that binary serialisation is discouraged as any change to field names breaks serialisation =? Not Good XMLSerializer is problematic because you have to provide a no arg constructor and public fields although you do have more control over elements being attributes or elements and their naming DataContractSerializer is good but all suclassses need to be explicitly added which is a shame However I stumbled across NetDataContractSerializer which does not have this limitation. If your goal is C# serialisation and no big constraints on size of xml

How to deserialize JSON with unnamed collection of types using DataContractSerializer

心已入冬 提交于 2019-11-30 09:22:07
问题 I'm using web service to get data about route mileage. Then I'm using deserializer to parse it out. Here is how JSON looks: [{"__type":"CalculateMilesReport:http:\/\/pcmiler.alk.com\/APIs\/v1.0","RouteID":null,"TMiles":445.5] With this response I had couple issues. Why is is wrapped into collection and how do I set object model? Also it was complaining about special __type attribute. So, I did "hack" and "prepped" string: // Cut off first and last charachters [] - they send objects as arrays

Passing an instance of anonymous type over WCF

ε祈祈猫儿з 提交于 2019-11-30 08:10:21
I have a WCF service method that expects an object and then retrieves its properties using reflection. On the client side I create an anonymous type object var obj = new {FirstName="John", LastName="Doe"} and pass it to the method. I'm getting an exception: Type '<>f__AnonymousType0`2[System.String,System.String]' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. See the Microsoft .NET Framework documentation for other supported types. I can't mark the type or its

Set default value in a DataContract?

笑着哭i 提交于 2019-11-30 06:18:58
How can I set a default value to a DataMember for example for the one shown below: I want to set ScanDevice="XeroxScan" by default [DataMember] public string ScanDevice { get; set; } I've usually done this with a pattern like this: [DataContract] public class MyClass { [DataMember] public string ScanDevice { get; set; } public MyClass() { SetDefaults(); } [OnDeserializing] private void OnDeserializing(StreamingContext context) { SetDefaults(); } private void SetDefaults() { ScanDevice = "XeroxScan"; } } Don't forget the OnDeserializing, as your constructor will not be called during

Simple data file versioning with DataContractSerializer

ぃ、小莉子 提交于 2019-11-30 05:21:26
问题 Having read Data Contract Versioning we concluded that it's not really the whole story. For example, what happens if you used to have ValueA, and in the new version it's now called ValueB and is of a different type, and you need to convert ValueA to ValueB? There are some callbacks I could use to help with this, but it doesn't look like a very maintainable solution if we expect the format to change frequently over a long period of time. The solution we settled for is to keep a "saved by

WCF Client having problems recognizing ServiceKnownTypes?

旧时模样 提交于 2019-11-30 04:02:05
问题 How would I tell the WCF service what KnownTypes to use when passing data back to the client? I know I can use the [ServiceKnownType] attribute, which makes the service call run fine from a WCF Test Server, however it still fails from the client. Am I missing something here? [OperationContract] [ServiceKnownType(typeof(SubClassA))] [ServiceKnownType(typeof(SubClassB))] BaseClassZ GetObject(); Error message from client is: {"Element 'http://schemas.datacontract.org/2004/07/BaseClassZ' contains

The deserializer has no knowlege of any type that maps to this contract

一世执手 提交于 2019-11-29 15:19:20
I'm trying to serialize and deserialize a tree of Node objects. My abstract "Node" class as well as other abstract and concrete classes that derive from it are defined in my "Informa" project. In addition, I've created a static class in Informa for serialization / deserialization. First I'm deconstructing my tree into a flat list of type Dictionary(guid,Node) where guid is the unique id of Node. I am able to serialize all my Nodes with out a problem. But when I try to deserialize I get the following exception. Error in line 1 position 227. Element ' http://schemas.microsoft.com/2003/10

Passing an instance of anonymous type over WCF

て烟熏妆下的殇ゞ 提交于 2019-11-29 11:11:00
问题 I have a WCF service method that expects an object and then retrieves its properties using reflection. On the client side I create an anonymous type object var obj = new {FirstName="John", LastName="Doe"} and pass it to the method. I'm getting an exception: Type '<>f__AnonymousType0`2[System.String,System.String]' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. See