Cannot deserialize the current JSON array (e.g. [1,2,3]) into type

It looks like the string contains an array with a single MyStok object in it. If you remove square brackets from both ends of the input, you should be able to deserialize the data as a single object: MyStok myobj = JSON.Deserialize<MyStok>(sc.Substring(1, sc.Length-2)); You could also deserialize the array into a list of MyStok objects, … Read more

Why my exception class needs to be serialized?

This is because the root class for all exceptions, Throwable implements the Serializable interface. All exceptions by default are serializable and that’s a language design decision because the authors wanted exceptions to be capable of being sent across the wire without any special configuration. If the base class is not serializable, you would have a … Read more

How do I write a custom JSON deserializer for Gson?

I’d take a slightly different approach as follows, so as to minimize “manual” parsing in my code, as unnecessarily doing otherwise somewhat defeats the purpose of why I’d use an API like Gson in the first place. // output: // [User: id=1, name=Jonas, updateDate=2011-03-24 03:35:00.226] // [User: id=5, name=Test, updateDate=2011-05-07 08:31:38.024] // using java.sql.Timestamp public … Read more

Serializing/deserializing with memory stream

This code works for me: public void Run() { Dog myDog = new Dog(); myDog.Name= “Foo”; myDog.Color = DogColor.Brown; System.Console.WriteLine(“{0}”, myDog.ToString()); MemoryStream stream = SerializeToStream(myDog); Dog newDog = (Dog)DeserializeFromStream(stream); System.Console.WriteLine(“{0}”, newDog.ToString()); } Where the types are like this: [Serializable] public enum DogColor { Brown, Black, Mottled } [Serializable] public class Dog { public String Name … Read more

Polymorphism in jackson annotations: @JsonTypeInfo usage

@JsonSubTypes.Type must have a value and a name like this, @JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=As.WRAPPER_OBJECT, property=”type”) @JsonSubTypes({ @JsonSubTypes.Type(value=Dog.class, name=”dog”), @JsonSubTypes.Type(value=Cat.class, name=”cat”) }) In the subclass, use @JsonTypeName(“dog”) to say the name. The values dog and cat will be set in the property named type.

How to handle a NumberFormatException with Gson in deserialization a JSON response

Here is an example that I made for Long type. This is a better option: public class LongTypeAdapter extends TypeAdapter<Long> { @Override public Long read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull(); return null; } String stringValue = reader.nextString(); try { Long value = Long.valueOf(stringValue); return value; } catch (NumberFormatException e) { … Read more

What are @JsonTypeInfo and @JsonSubTypes used for in jackson

The purpose of these annotations is to support polymorphism on deserialization. When deserializing the actual code being executed will know the class of what it expects. E.g., the type of some field being deserialized into. But if that class has subclasses (i.e., subtypes) how does the generic Jackson deserializer know which actual class the string … Read more

What’s the Jackson deserialization equivalent of @JsonUnwrapped?

You can use @JsonCreator with @JsonProperty for each field: @JsonCreator public Parent(@JsonProperty(“age”) Integer age, @JsonProperty(“firstName”) String firstName, @JsonProperty(“lastName”) String lastName) { this.age = age; this.name = new Name(firstName, lastName); } Jackson does type checking and unknown field checking for you in this case.

java.io.InvalidClassException: local class incompatible:

If a class does not explicitly define a private static final long serialVersionUID in the code it will be autogenerated, and there is no guarantee that different machines will generate the same id; it looks like that is exactly what happened. Also if the classes are different in any way (using different versions of the … Read more

Is json.loads() vulnerable to arbitrary code execution?

Note that the below answer is relating to the default Python3.4 installation for Windows 10 64-bit. Also note that this answer only looks at the py scanner, not the c scanner. For the source files see https://hg.python.org/cpython/file/tip/Lib/json or find them in your local python installation. Research See the reference implementation at the bottom of this … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)