6

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!

user1955934
  • 3,185
  • 5
  • 42
  • 68

2 Answers2

2

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));
user1955934
  • 3,185
  • 5
  • 42
  • 68
1

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];
Amila
  • 5,195
  • 1
  • 27
  • 46
  • Thanks Amila, I've tried adding that but it still wouldn't be serialized. I have checked inside my database and indeed the location data is there, just not getting serialized into json... – user1955934 Sep 26 '15 at 13:37