datacontractserializer

Conditional DataContract Serialization in WebApi

人盡茶涼 提交于 2019-12-11 00:35:14
问题 I am trying to find the best way to conditionally include and remove properties from my datacontract serialization in my .net WebApi project. In my Api, I want to allow users to specify the fields that they want returned. For example, assume I want my API to return an instance of the following class. public class Car { public int Id { get; set; } public string Year { get; set; } public string Make { get; set; } public string Model { get; set; } public string Color { get; set; } } But instead

DataContractSerializer and Known Types

人走茶凉 提交于 2019-12-10 21:34:10
问题 I am serializing an object in code (not via a WCF call), and I am getting a little hung up with known types (I have used them with WCF, but not with the DataContract serializer as a "stand-alone" serializer) I get an exception when I run the code below. I expected it to run without error because I supplied Known Types. What am I getting wrong here? public class Parent {} public class Child: Parent {} // the code -- serialized is a serialized Child // type is typeof(Parent) (I know it works if

How to use DataContractSerializer to create xml with tag names that match my known types

折月煮酒 提交于 2019-12-10 20:00:06
问题 I have the following data contract: [CollectionDataContract(Name="MyStuff")] public class MyStuff : Collection<object> {} [DataContract(Name = "Type1")] [KnownType(typeof(Type1))] public class Type1 { [DataMember(Name = "memberId")] public int Id { get; set; } } [DataContract(Name = "Type2")] [KnownType(typeof(Type2))] public class Type2 { [DataMember(Name = "memberId")] public int Id { get; set; } } Which I serialize to xml as follows: MyStuff pm = new MyStuff(); Type1 t1 = new Type1 { Id =

Can DataContractJsonSerializer handle cyclic references?

夙愿已清 提交于 2019-12-10 10:47:59
问题 Are there any serialization/deserialization scenarios that DataContractSerializer can handle, while DataContractJsonSerializer can't ? In particular, I am thinking of circular references: this MSDN page explains how circular references can be managed by DataContractSerializer via the use of IsReference = true in the DataContractAttribute constructor. On the other hand, the DataContractAttribute.IsReference documentation does not explicitly state that its applicability is limited to

Serializing cyclic object references using DataContractSerializer not working

戏子无情 提交于 2019-12-10 10:17:11
问题 I'm building an XNA game and I'm trying to save game/map etc. state completely, and then be able to load and resume from exactly the same state. My game logic consists of fairly complex elements (for serializing) such as references, delegates etc. I've done hours of research and decided that it's the best to use a DataContractSerializer that preserves the object references. (I also got around for delegates but that's another topic) I have no problem serializing and deserializing the state, re

DataContract model binding to JSON in ASP.NET MVC Action Method Arguments

我的未来我决定 提交于 2019-12-09 17:34:56
问题 MVC3 comes out of the box with JsonValueProviderFactory() which is very handy for binding incoming JSON to a model. Unfortunately, I can't figure out how to setup model contracts with names that differ from the incoming JSON. For example: [DataContract(Name = "session")] public class FacebookSession { [DataMember(Name = "access_token")] public string AccessToken { get; set; } [DataMember(Name = "expires")] public int? Expires { get; set; } [DataMember(Name = "secret")] public string Secret {

DataContracts and DataMembers

百般思念 提交于 2019-12-09 08:43:12
问题 Are there any ways to tell WCF to serialize the whole class when it returns? Do I literally have to add DataMember to every property? 回答1: Since .NET 3.5 SP1, you don't have to do this anymore. If you don't have any [DataContract] and [DataMember] attributes, the DataContractSerializer class will behave like the old XmlSerializer: it will serialize all public read/write properties that are listed in the class. You do lose a few things in the process though: since you don't have [DataMember]

DataContractSerializer with Multiple Namespaces

扶醉桌前 提交于 2019-12-08 17:24:36
问题 I am using a DataContractSerializer to serialize an object to XML. The main object is SecurityHolding with the namespace "http://personaltrading.test.com/" and contains a property called Amount that's a class with the namespace "http://core.test.com". When I serialize this to XML I get the following: <ArrayOfSecurityHolding xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://personaltrading.test.com/"> <SecurityHolding> <Amount xmlns:d3p1="http://core.test.com/"> <d3p1:Amount>1

How can I serialise a string array to JSON using DataContractJsonSerializer?

旧城冷巷雨未停 提交于 2019-12-08 08:11:00
问题 This seems to be the same question as below but that answer hasn't resolved it: Deserializing a Simple JSON Array I am using DataContractJsonSerializer to convert from XML to JSON and vice versa. Everything works with complex data types and arrays of complex data types but I'm having a problem producing JSON for a string array. The JSON I need to produce should have this structure: { "data": { "x_axis": { "labels": [ "Jan", "Feb", "Mar", "Apr","May", "Jun", "Jul", Aug","Sep", Oct", "Nov",

Nested [DataContract]'s not serialising through WCF

半城伤御伤魂 提交于 2019-12-08 08:06:16
问题 I'm having an issue where a class decorated with the [DataContract] attribute and appropriate [DataMember] attributes on properties is not serialising nested [DataContract] classes. This is the class i'm trying to serialise: [DataContract(Namespace = "http://foo.bar.com.au")] [KnownType(typeof(Point))] [KnownType(typeof(Site))] public sealed class Alarm : IExtensibleDataObject { [DataMember] public Point SourcePoint { get; set; } [DataMember] public Site SourceSite { get; set; } [DataMember]