serialization

PHP: __toString() and json_encode() not playing well together

笑着哭i 提交于 2019-12-20 19:38:13
问题 I've run into an odd problem and I'm not sure how to fix it. I have several classes that are all PHP implementations of JSON objects. Here' an illustration of the issue class A { protected $a; public function __construct() { $this->a = array( new B, new B ); } public function __toString() { return json_encode( $this->a ); } } class B { protected $b = array( 'foo' => 'bar' ); public function __toString() { return json_encode( $this->b ); } } $a = new A(); echo $a; The output from this is [{},{

Insert Dictionary into MongoDB with c# driver

我是研究僧i 提交于 2019-12-20 18:40:16
问题 I am in a situation where I can't predict which fields my MongoDB document is going to have. So I can no longer create an object with an _id field of type BsonID . I find it very convenient to create a Dictionary (HashTable) and add my DateTime and String objects inside, as many times as I need it. I then try to insert the resulting Dictionary object into MongoDb, but default serialization fails. Here's my object of Type HashTable (like a Dictionary, but with varied types inside): { "_id":"",

Why classes are not serializable by default in .Net?

只谈情不闲聊 提交于 2019-12-20 17:39:49
问题 Developers have to 'opt in' for making classes serializable by explicitly using SerializableAttribute . What could go wrong if classes were serializable by default? 回答1: I would assume that classes are not serializable by default because there's no guarantee that a dump of the object's state to a stream using Reflection even makes sense. What if your object holds an open connection to a database or communication port? Whenever a new object was constructed by deserializing an instance of the

What are @JsonTypeInfo and @JsonSubTypes used for in jackson

穿精又带淫゛_ 提交于 2019-12-20 17:25:37
问题 What are @JsonTypeInfo and @JsonSubTypes annotations using for in jackson ? public class Lion extends Animal { private String name; @JsonCreator public Lion(@JsonProperty("name") String name) { this.name = name; } public String getName() { return name; } public String getSound() { return "Roar"; } public String getType() { return "carnivorous"; } public boolean isEndangered() { return true; } @Override public String toString() { return "Lion [name=" + name + ", getName()=" + getName() + ",

Typescript objects serialization?

泄露秘密 提交于 2019-12-20 16:16:35
问题 Are there any means for JSON serialization/deserialization of Typescript objects so that they don't loose type information? Simple JSON.parse(JSON.stringify) has too many caveats. Or I should use adhoc solutions? 回答1: Use Interfaces to get strong types: // Creating var foo:any = {}; foo.x = 3; foo.y='123'; var jsonString = JSON.stringify(foo); alert(jsonString); // Reading interface Bar{ x:number; y?:string; } var baz:Bar = JSON.parse(jsonString); alert(baz.y); And use type assertion "<>" if

C# Deserialize a class which has moved or been renamed

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-20 13:59:16
问题 If I have a class named "MyClass" in an assembly named "AssemblyA" and serialize it to a file using .NET's BinaryFormatter. Then move the code of "MyClass" into an assembly named "AssemblyB" and try to deserialize the file I get the following "System.TypeLoadException" exception: Could not load type 'AssemblyA.MyClass' from assembly 'AssemblyA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Is there any way for me to indicate that the class has been moved to AssemblyB? Via an

A minimalistic human-readable serialisation format parser for an embedded system

坚强是说给别人听的谎言 提交于 2019-12-20 12:35:39
问题 By "human-readable serialisation format" I mean YAML , JSON , INI or like. Please note, XML is too verbose and too inconvenient for my purposes, so let's leave it alone as the last resort. The format should store the data as "named key -- value" pairs and allow for nesting and arrays. Absence of arrays is not critical, though. Also, type-awareness (ability to return data not only as plain strings) is highly appreciated. What I need exactly is a pure C library, which provides an API for

A minimalistic human-readable serialisation format parser for an embedded system

天涯浪子 提交于 2019-12-20 12:35:09
问题 By "human-readable serialisation format" I mean YAML , JSON , INI or like. Please note, XML is too verbose and too inconvenient for my purposes, so let's leave it alone as the last resort. The format should store the data as "named key -- value" pairs and allow for nesting and arrays. Absence of arrays is not critical, though. Also, type-awareness (ability to return data not only as plain strings) is highly appreciated. What I need exactly is a pure C library, which provides an API for

Is there a built in way in .Net AJAX to manually serialize an object to a JSON string?

时间秒杀一切 提交于 2019-12-20 12:32:08
问题 I've found ScriptingJsonSerializationSection but I'm not sure how to use it. I could write a function to convert the object to a JSON string manually, but since .Net can do it on the fly with the <System.Web.Services.WebMethod()> and <System.Web.Script.Services.ScriptMethod()> attributes so there must be a built-in way that I'm missing. PS: using Asp.Net 2.0 and VB.Net - I put this in the tags but I think people missed it. 回答1: This should do the trick Dim jsonSerialiser As New System.Web

Spring and JacksonJson, serialising different fields with views

牧云@^-^@ 提交于 2019-12-20 12:31:44
问题 In a previous similar question, I asked about, how to serialise two different sets of fields using JacksonJson and Spring. My use case is the typical Controller mapping with @ResponseBody annotation returning directly a particular object or collections of objects, that are then rendered with JacksonJson whenever the client adds application/json in the Accept header. I had two answers, the first one suggests to return different interfaces with a different getter list, the second suggests to