0

I have a POJO in my application:

class Project{
   long id;
   String name;
   File[] images;
   //getter and stter omitted
}

And I use the form to add/update the Project

<form>
  <input name="project.name" .../>
  <input name="project.images[0]  />
  ...
</form>

It works well with CRUD operation exception once I want to get the Project with json format.

I want to get something like this:

{name:"projectname",id:1,images:["http://xx.png","..."]}

But I can not since the images filed have the type of File rather than String.

I can add another fields to hold the image url like this:

class Project{
   long id;
   String name;
   File[] images;
   String[] imageURLs;
   //getter and stter omitted
}

But I am not sure if this is a good idea, since two fields are used to represent the same thing.

I wonder if there is a better alternative solution?

hguser
  • 35,079
  • 54
  • 159
  • 293
  • what is your problem? output json or output the url, If latter then i think using a field for url is not a bad idea, they are different things, the custom url and the original path of image. and if first one then use a library like org.json – Harry Aug 08 '14 at 00:22

1 Answers1

0

You need a custom deserializer for your File[]. See GSON deserializing key-value to custom object for more details.

Community
  • 1
  • 1
nmore
  • 2,474
  • 1
  • 14
  • 21
  • How about if I do not use GSON? – hguser Aug 08 '14 at 00:09
  • What Json library are you using? – nmore Aug 08 '14 at 00:10
  • In fact I am using a thrid-party library which use their own json implementation to do the deserialization and serialization job. – hguser Aug 08 '14 at 00:16
  • Yes I know that, but what is the name of the library? Decent ones have a way to configure the custom deserialization. – nmore Aug 08 '14 at 00:19
  • Sorry, but I can't help you if you aren't giving me the information that I need... – nmore Aug 08 '14 at 00:23
  • No, thank you anyway, I just check the library, and I do not find anything to configure the deseriazation. Maybe I should just add another field. – hguser Aug 08 '14 at 00:30
  • Ok cool, if you are sure there is nothing like that then you need a String[] like you said. – nmore Aug 08 '14 at 01:10