serialization

Django: Serializing a list of multiple, chained models

試著忘記壹切 提交于 2020-01-02 04:33:53
问题 Given two different models, with the same parent base class. Is there any way, using either Django Rest Framework Serializers or serpy, to serialize a chained list containing instances of both the child models? Given some example models: class BaseModel(models.Model): created_at = models.DateField() class Meta: abstract = True class ChildModel1(BaseModel): field_one = models.TextField() class ChildModel2(BaseModel): field_two = models.TextField() And an example view: def get(self, request):

Swift Generic Object/JSON Serialization

ε祈祈猫儿з 提交于 2020-01-02 03:37:08
问题 tl;dr Can you generically instantiate an object using a conforming protocol's initializer method while also still preserving the object's intended type? What I am trying now is seg faulting the compiler. In a library I am writing, I am trying to accomplish a goal of generically serializing/deserializing objects using Swift's limited introspection features. Here is the code for a global function that sets a variable's value using reflection. It attempts to reconcile for nested constructs if it

Problems deserializing List of objects

感情迁移 提交于 2020-01-02 03:35:19
问题 I am having trouble deserializing a list of objects. I can get just one object to serialize into an object but cannot get the list. I get no error it just returns an empty List. This is the XML that gets returned: <locations> <location locationtype="building" locationtypeid="1"> <id>1</id> <name>Building Name</name> <description>Description of Building</description> </location> </locations> This is the class I have and I am deserializing in the GetAll method: [Serializable()] [XmlRoot(

Programmatically serialize class to xsd

扶醉桌前 提交于 2020-01-02 03:29:10
问题 Is there a way to create an XSD from a C# .NET class programmatically? I want to serialize objects to xsd (or xml) with type information. 回答1: Yes; look at XsdDataContractExporter ; MSDN has a full example here. Alternative; XmlSchemaExporter 回答2: This should give you the types as well! (if you are looking for xml solution, for xsd solution, Marc has the answer ;-)) var oEmp = new Emp { FirstName = "John", LastName = "Smith", DOJ = DateTime.Today }; using (var stream = File.Create("J:\\XML\

Replacing class name in serialized data

99封情书 提交于 2020-01-02 01:59:14
问题 I want to replace the String "com.oldpackage.className" with "com.newPackage.className" in a stream of serialized data. This serialized data is read from the DB and updated after replacing the string. I am facing some problems while doing the same. If you already guessed it, this is part of a refactoring exercise. Are there any libraries that would help me in manipulating the serialized data? If you can also please comment on any precautions or caveats, it would be of great help. Thanks a lot

How to control json_encode behavior?

你说的曾经没有我的故事 提交于 2020-01-02 01:29:30
问题 Is there any way to control json_encode behavior on objects? Like excluding empty arrays, null fields and so on? I mean something like when using serialize() , where you can implement magic __sleep() method and specify what properties should be serialized: class MyClass { public $yes = "I should be encoded/serialized!"; public $empty = array(); // // Do not encode me! public $null = null; // Do not encode me! public function __sleep() { return array('yes'); } } $obj = new MyClass(); var_dump

Cast object to a generic type

只谈情不闲聊 提交于 2020-01-02 01:24:08
问题 I haven't slept in a while so this is probably easier than I think it is. I have a generic class that's more or less this: public class Reference<T> where T : APIResource //<- APIResource is abstract btw { private T _value = null; public T value { get { return _value; } } } Elsewhere, in a custom serialize method, someone is passing in an object that is actually an instance of Reference<(something)> . I simply want to skip to the "value" property that every Reference<> object has, so I want

Is this bug ?…got reason behind circular reference … how to solve but?

≡放荡痞女 提交于 2020-01-02 00:20:14
问题 i have asp.net mvc application where my model has a relation like "Question can have multiple answers". So while creating its .dbml file and classes question class would contain the EntitySet right? but each object in the EntitySet (means Answer object) will having the Property as "Question" , so framework automatically creates there circular reference and dependencies. which does comes in focus when we going to serialize the List of Question (List) to generate the json output, for particular

How can I serialize an std::vector with boost::serialization?

会有一股神秘感。 提交于 2020-01-02 00:14:32
问题 class workflow { private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & tasks; ar & ID; } vector<taskDescriptor> tasks; int ID; How can i serialize the member "tasks" using boost libraries? 回答1: #include <boost/serialization/vector.hpp> Also read tutorial. 回答2: Just to add a generic example to this question. Lets assume we want to serialize a vector without any classes or anything. This is how you can do it:

Serializing Immutable Value types with Mongo C# Driver

て烟熏妆下的殇ゞ 提交于 2020-01-01 18:17:15
问题 I have many immutable value type classes, for example EmailAddress , which ensure any non null instance is valid. I would like to control the serialization of these types of objects to be just the standard string representation ( "123@abc.com" ) when persisted using MongoDB C# Driver. I have tried implementing the IBsonSerilizer however it will only allow for objects or arrays at the root level. I was able to implement proper Json Serilization with Json.NET, is there a different approach I