FlexJson - Unable to Serialize Double[] array

255 views Asked by At

I have a simple user class with a Double[] variable for specifying a user's location.

@Document
public class User {
    private long id;
    private Double[] location;
}

This is the code that i have tried with to serialize my User object

new JSONSerializer()
           .transform(new ArrayTransformer(), Double[].class)
           .serialize(object));

But the location field won't get serialized, other fields are serialized though.. Could someone please help?

Thanks!

2

There are 2 answers

1
Amila On

Just declaring the variable isn't enough since it's initialized to null by default.

Either set a value using setter method or initialize it with and empty array, for example:

private Double[] location = new Double[10];
0
user1955934 On

After much trying, I finally managed to make it work by explicitly including the field:

final String[] includedFields = {"location"}; 

new JSONSerializer()
                    .include(includedFields)
                    .serialize(object));