0

I've a requirement to convert a set of file structure obtained from DB into a JSON. For example: From DB, I get the following path and file attributes:

Record 1:
    "path": "/sd/card/camera/pics/selfie.jpg"
    "fileName": "selfie.jpg",
    "mimeType": "image/jpeg"

Record 2:
    "path": "/sd/card/camera/pics/personal/selfie1.jpg"
    "fileName": "selfie1.jpg",
    "mimeType": "image/jpeg"

and so on.. I need to convert this to a JSON like:

 [{
    "sd": [{
        "card": [{
            "camera": [{
                "pics": [{
                        "fileName": "selfie.jpg",
                        "path": "/sd/card/camera/pics/selfie.jpg",
                        "mimeType": "image/jpeg"
                    },
                    {
                        "personal": [{
                            "fileName": "selfie1.jpg",
                            "path": "/sd/card/camera/pics/personal/selfie1.jpg",
                            "mimeType": "image/jpeg"
                        }]
                    }
                ]
            }]
        }]
    }]
}]
3
  • 1
    what have you tried?... paste some code here Commented Jul 4, 2017 at 7:12
  • Tried the code here: stackoverflow.com/questions/10660167/… But this gives back children object, while I need a list Commented Jul 4, 2017 at 7:26
  • @Sinistro any idea on how to convert this into a tree structure? Commented Jul 5, 2017 at 4:12

1 Answer 1

1

I'm going to give you a 'jackson' solution.

First, build an object (or more, I let you deal with the Java inheritance, or use any kind of structure you want to use). Like this one by example :

 @JsonSerialize(using = CustomSerializer.class)
public class Something {

    private String currentFolder; // Name of the folder if this instance of something is a folder
    private Something[] childs;

    private Map<String,String> currentPicture; // Picture properties if this instance of something is a picture

    public Something() {currentPicture = new HashMap<String,String>();}

    public Something[] getChilds() {
        return childs;
    }

    public void setContent(Something[] _childs) {this.childs = _childs;}
    public String getCurrentFolder() {return currentFolder;}
    public void setCurrentFolder(String _currentFolder) {this.currentFolder = _currentFolder;}
    public Map<String,String> getCurrentPicture() {return currentPicture;}
    public void setCurrentPicture(Map<String,String> currentPicture) {this.currentPicture = currentPicture;}
}

Then, create the CustomSerializer, that will help you do whatever you want to do :

    public class CustomSerializer extends JsonSerializer<Something>{

    @Override
    public void serialize(Something value, JsonGenerator jgen, SerializerProvider provider)
            throws IOException, JsonProcessingException {
        jgen.writeStartObject();
        // Adding the folder into the json, only if it exists
        if(value.getCurrentFolder()!=null){
            jgen.writeObjectField(value.getCurrentFolder(), value.getChilds());
        }

        // Adding properties of the picture, only if they exist
        if(value.getCurrentPicture()!= null){
            for(String k : value.getCurrentPicture().keySet()){
                jgen.writeObjectField(k,value.getCurrentPicture().get(k));
            }
        }
        jgen.writeEndObject();
    }

}

Finally (I've not done this one, but you'll do it I'm sure !) create a mapper from what you read to the "Something" class. I build the object manually here (quickly, so it's not clean):

public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
    Something s = new Something();
    s.setCurrentFolder("toto");
    Something s2 = new Something();
    s2.setCurrentFolder("tata");

    Something s2bis = new Something();
    s2bis.setCurrentFolder("tataBis");

    Something[] s2Group = {s2bis};
    s2.setContent(s2Group);
    Something s2bispic = new Something();
    s2bispic.getCurrentPicture().put("fileName", "ThatPictureOfMysSelfILikeSoMuch.jpg");
    s2bispic.getCurrentPicture().put("path", "toto/tata/tataBis/ThatPictureOfMysSelfILikeSoMuch.jpg");
    s2bispic.getCurrentPicture().put("mimeType", "image/jpeg");

    Something s2bispic2 = new Something();
    s2bispic2.getCurrentPicture().put("fileName", "ThatPictureOfMysSelfIDontLike.jpg");
    s2bispic2.getCurrentPicture().put("path", "toto/tata/tataBis/ThatPictureOfMysSelfIDontLike.jpg");
    s2bispic2.getCurrentPicture().put("mimeType", "image/jpeg");


    Something[] s2BisGroup = {s2bispic,s2bispic2};
    s2bis.setContent(s2BisGroup);
    Something s3 = new Something();

    s3.getCurrentPicture().put("fileName", "selfie.jpg");
    s3.getCurrentPicture().put("path", "toto/selfie.jpg");
    s3.getCurrentPicture().put("mimeType", "image/jpeg");

    Something[] sGroup = {s2,s3};
    s.setContent(sGroup);

    ObjectMapper mapper = new ObjectMapper();
    String temp = mapper.writeValueAsString(s);
    System.out.println(temp);
}

And this is what I get :

    {
   "toto":[
      {
         "tata":[
            {
               "tataBis":[
                  {
                     "path":"toto/tata/tataBis/ThatPictureOfMysSelfILikeSoMuch.jpg",
                     "fileName":"ThatPictureOfMysSelfILikeSoMuch.jpg",
                     "mimeType":"image/jpeg"
                  },
                  {
                     "path":"toto/tata/tataBis/ThatPictureOfMysSelfIDontLike.jpg",
                     "fileName":"ThatPictureOfMysSelfIDontLike.jpg",
                     "mimeType":"image/jpeg"
                  }
               ]
            }
         ]
      },
      {
         "path":"toto/selfie.jpg",
         "fileName":"selfie.jpg",
         "mimeType":"image/jpeg"
      }
   ]
}

Regards,

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Grai . But in my case I can have nth depth, and in that case, object structure won't work. Need a tree structure kind of solution. And then, I can use the Gson to convert it.
Hi, Yeah, tree solution might be cleaner. The most important is that you can use that custom serializer to serialize your data object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.