serialization

Django REST Framework - Serializing optional fields

心不动则不痛 提交于 2019-12-30 01:39:13
问题 I have an object that has optional fields. I have defined my serializer this way: class ProductSerializer(serializers.Serializer): code = serializers.Field(source="Code") classification = serializers.CharField(source="Classification", required=False) I thought required=False would do the job of bypassing the field if it doesn't exist. However, it is mentioned in the documentation that this affects deserialization rather than serialization. I'm getting the following error: 'Product' object has

Django REST Framework - Serializing optional fields

亡梦爱人 提交于 2019-12-30 01:39:08
问题 I have an object that has optional fields. I have defined my serializer this way: class ProductSerializer(serializers.Serializer): code = serializers.Field(source="Code") classification = serializers.CharField(source="Classification", required=False) I thought required=False would do the job of bypassing the field if it doesn't exist. However, it is mentioned in the documentation that this affects deserialization rather than serialization. I'm getting the following error: 'Product' object has

Java serialization - Android deserialization

北慕城南 提交于 2019-12-30 01:27:06
问题 I have tried implementing cross platform serialization between Java and Android. I have used Serializable, and having my code in Android in the same package as in desktop Java. Source: java-desktop serializing Student student=new Student(); student.setName("John"); student.setSurname("Brown"); student.setNumber(776012345); try { FileOutputStream fout = new FileOutputStream("thestudent.dat"); ObjectOutputStream oos = new ObjectOutputStream(fout); oos.writeObject(student); oos.close(); } catch

Django REST framework flat, read-write serializer

☆樱花仙子☆ 提交于 2019-12-29 19:18:51
问题 In Django REST framework, what is involved in creating a flat, read-write serializer representation? The docs refer to a 'flat representation' (end of the section http://django-rest-framework.org/api-guide/serializers.html#dealing-with-nested-objects) but don't offer examples or anything beyond a suggestion to use a RelatedField subclass. For instance, how to provide a flat representation of the User and UserProfile relationship, below? # Model class UserProfile(models.Model): user = models

Django REST framework flat, read-write serializer

不问归期 提交于 2019-12-29 19:18:10
问题 In Django REST framework, what is involved in creating a flat, read-write serializer representation? The docs refer to a 'flat representation' (end of the section http://django-rest-framework.org/api-guide/serializers.html#dealing-with-nested-objects) but don't offer examples or anything beyond a suggestion to use a RelatedField subclass. For instance, how to provide a flat representation of the User and UserProfile relationship, below? # Model class UserProfile(models.Model): user = models

Django REST framework flat, read-write serializer

家住魔仙堡 提交于 2019-12-29 19:17:13
问题 In Django REST framework, what is involved in creating a flat, read-write serializer representation? The docs refer to a 'flat representation' (end of the section http://django-rest-framework.org/api-guide/serializers.html#dealing-with-nested-objects) but don't offer examples or anything beyond a suggestion to use a RelatedField subclass. For instance, how to provide a flat representation of the User and UserProfile relationship, below? # Model class UserProfile(models.Model): user = models

To initialize a transient field, what is the most simple solution

扶醉桌前 提交于 2019-12-29 16:29:08
问题 class MyClass implements Serializable { transient int myTransient; //Other variables } When I restore this class I want to initialize myTransient manually, but otherwise I just want to use the default serialization. How can I inject an init() method into the object restore process without re-writing the entire serialization mechanism as it seems like Externalizable would have me do? 回答1: Implement a readObject() method: private void readObject(java.io.ObjectInputStream in) throws IOException,

problem with floating values

主宰稳场 提交于 2019-12-29 09:52:23
问题 I ve got a strange php behaviour with floating value $array_test["test"]= round($value,2); //first I round up a value echo $array_test["test"]; //0,66 $s_array_test= serialize($array_test); //serializing the array var_dump($s_array_test) // (...)s:4:"test";d:0.66000000000000003108624468950438313186168670654296875;}(...) This is pretty annoying cause the serialized array is stored into a db a use more space... How to fix this ? thx 回答1: First of all read the section about floats in the manual

Serialization issue with SortedSet, Arrays, an Serializable

房东的猫 提交于 2019-12-29 09:23:11
问题 I have this before the process: protected void onPostExecute(SortedSet<RatedMessage> result) { List<Object> list=Arrays.asList(result.toArray()); lancon.putExtra("results", list.toArray()); // as serializable } then in the other part I have Object o=this.getIntent().getSerializableExtra("results"); //at this point the o holds the correct value (checked by debugger) RatedMessage[] rm = (RatedMessage[]) o;// this line hangs out w ClassCastException resultSet = new TreeSet<RatedMessage>(new Comp

C# Deserialize an object which is derived and has references

北城余情 提交于 2019-12-29 09:13:08
问题 I have a object of Type Node. Node.cs The serialization works when I make the call as following: var nodeSer = JsonConvert.SerializeObject(mynode, Formatting.Indented, new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects }); My problem is that the following call does not work. var n = JsonConvert.DeserializeObject<Node>(nodeSer, new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects, TypeNameHandling =