Direct self-reference leading to cycle exception

In this case you need to annotate the relationships with @JsonManagedReference and @JsonBackReference like this: @ManyToOne @JoinColumn(name = “company_id”, referencedColumnName = “id”) @JsonBackReference private Company company And @OneToMany(mappedBy=”company”) @JsonManagedReference private Set<Employee> employee = new HashSet<Employee>(); There is a nice example here

Can I serialize an ExpandoObject in .NET 4?

I can’t serialize ExpandoObject, but I can manually serialize DynamicObject. So using the TryGetMember/TrySetMember methods of DynamicObject and implementing ISerializable, I can solve my problem which was really to serialize a dynamic object. I’ve implemented the following in my simple test app: using System; using System.Windows.Forms; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using System.Collections.Generic; using … Read more

System.Version not serialized

System.Version is not serializable, if you look at it’s properties on MSDN, you’ll see they have no setters…so the serializer won’t store them. However, this approach still works. That article (old but still works) provides a Version class that is serializable, can you switch to that and get going? Edit by tomfanning I have fished … Read more

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

Implement a readObject() method: private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); myTransient = …; } From javadoc: Classes that require special handling during the serialization and deserialization process must implement special methods with these exact signatures: private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException; The readObject method is responsible for reading from the stream … Read more

Should a Log4J logger be declared as transient?

How about using a static logger? Or do you need a different logger reference for each instance of the class? Static fields are not serialized by default; you can explicitly declare fields to serialize with a private, static, final array of ObjectStreamField named serialPersistentFields. See Oracle documentation Added content: As you use getLogger(getClass()), you will … Read more

Gson add field during serialization

Use Gson.toJsonTree to get a JsonElement, with which you can interact dynamically. A a = getYourAInstanceHere(); Gson gson = new Gson(); JsonElement jsonElement = gson.toJsonTree(a); jsonElement.getAsJsonObject().addProperty(“url_to_user”, url); return gson.toJson(jsonElement);

How do I make JSON.NET ignore object relationships?

First, to address your issues with reference loops– The PreserveReferencesHandling setting controls whether Json.Net emits $id and $ref to track inter-object references. If you have this set to None and your object graph contains loops, then you will also need to set ReferenceLoopHandling to Ignore to prevent errors. Now, to get Json.Net to ignore all … Read more