Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting the unstructured object in java

I'm using MongoDb for unstructured documents. When I do the aggregations, I'm getting final output as unstructured objects. I post some sample data for the easiness. Actual objects have many fields. Eg :

[
    { _id : "1", type: "VIDEO", videoUrl : "youtube.com/java"},
    { _id : "2", type: "DOCUMENT", documentUrl : "someurl.com/spring-boot-pdf"},
    { _id : "3", type: "ASSESSMENT", marks : 78}
]

The respective class for the types of above objects are

@Data
public class Video{
    private String _id;
    private String type;
    private String videoUrl;
}

@Data
public class Document{
    private String _id;
    private String type;
    private String documentUrl;
}

@Data
public class Assessment{
    private String _id;
    private String type;
    private Integer marks;
}

Since I can't specify the converter class, I get all objects as list of Object.class which is a general type for all.

List<Object> list = mongoTemplate.aggregate(aggregation, mongoTemplate.getCollectionName(YOUR_COLLECTION.class), Object.class).getMappedResults();

It's working, but this is not readable and not maintainable for backend and front-end developers (eg : swagger ui). So I came up with a solution, that put all fields as a class.

@Data
@JsonInclude(JsonInclude.Include.NON_NULL) 
class MyConvetor{
    private String _id;
    private String type;
    private String videoUrl;
    private String documentUrl;
    private Integer marks;
}

Here Jackson helps to ignore all null fields

Now I can use MyConverter as Type

List<MyConverter> list = mongoTemplate.aggregate(aggregation, mongoTemplate.getCollectionName(YOUR_COLLECTION.class), MyConverter.class).getMappedResults();

But I feel this is not a good practice when we implementing a standard application. I'd like to know, is there any way to avoid the general type class (e.g. extending any abstract class)? Or is this the only way I can do?

like image 587
varman Avatar asked Oct 10 '20 18:10

varman


1 Answers

I don't think so (or I don't know) if MongoDB in Java provides this kind of dynamic conversion by some field (it would require specify what field and what classes). But you can do it by hand.

First, you need to define your types (enum values or some map) for matching string to class. You can create abstract parent class (eg. TypedObject) for easier usage and binding all target classes (Video, Document, Assessment) .

Next you have to read and map values from Mongo to anything because you want to read all data in code. Object is good but I recommend Map<String, Object> (your Object actually is that Map - you can check it by invoking list.get(0).toString()). You can also map to String or DBObject or some JSON object - you have to read "type" field by hand and get all data from object.

At the end you can convert "bag of data" (Map<String, Object> in my example) to target class.

Now you can use converted objects by target classes. For proving these are actually target classes I print objects with toString all fields.

Example implementation

Classes:

@Data
public abstract class TypedObject {
    private String _id;
    private String type;
}

@Data
@ToString(callSuper = true)
public class Video extends TypedObject {
    private String videoUrl;
}

@Data
@ToString(callSuper = true)
public class Document extends TypedObject {
    private String documentUrl;
}

@Data
@ToString(callSuper = true)
public class Assessment extends TypedObject {
    private Integer marks;
}

Enum for mapping string types to classes:

@RequiredArgsConstructor
public enum Type {
    VIDEO("VIDEO", Video.class),
    DOCUMENT("DOCUMENT", Document.class),
    ASSESSMENT("ASSESSMENT", Assessment.class);

    private final String typeName;
    private final Class<? extends TypedObject> clazz;

    public static Class<? extends TypedObject> getClazz(String typeName) {
        return Arrays.stream(values())
                .filter(type -> type.typeName.equals(typeName))
                .findFirst()
                .map(type -> type.clazz)
                .orElseThrow(IllegalArgumentException::new);
    }
}

Method for converting "bag of data" from JSON to your target class:

    private static TypedObject toClazz(Map<String, Object> objectMap, ObjectMapper objectMapper) {
        Class<? extends TypedObject> type = Type.getClazz(objectMap.get("type").toString());
        return objectMapper.convertValue(objectMap, type);
    }

Read JSON to "bags of data" and use of the above:

    String json = "[\n" +
            "    { _id : \"1\", type: \"VIDEO\", videoUrl : \"youtube.com/java\"},\n" +
            "    { _id : \"2\", type: \"DOCUMENT\", documentUrl : \"someurl.com/spring-boot-pdf\"},\n" +
            "    { _id : \"3\", type: \"ASSESSMENT\", marks : 78}\n" +
            "]";

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);

    List<Map<String, Object>> readObjects = objectMapper.readValue(json, new TypeReference<>() {});

    for (Map<String, Object> readObject : readObjects) {
        TypedObject convertedObject = toClazz(readObject, objectMapper);
        System.out.println(convertedObject);
    }

Remarks:

  • In example I use Jackson ObjectMapper for reading JSON. This makes the example and testing simpler. I think you can replace it with mongoTemplate.aggregate(). But anyway I need ObjectMapper in toClazz method for converting "bags of data".
  • I use Map<String, Object> instead of just Object. It is more complicated: List<Map<String, Object>> readObjects = objectMapper.readValue(json, new TypeReference<>() {});. If you want, you can do something like this: List<Object> readObjects2 = (List<Object>) objectMapper.readValue(json, new TypeReference<List<Object>>() {});

Result:

Video(super=TypedObject(_id=1, type=VIDEO), videoUrl=youtube.com/java)
Document(super=TypedObject(_id=2, type=DOCUMENT), documentUrl=someurl.com/spring-boot-pdf)
Assessment(super=TypedObject(_id=3, type=ASSESSMENT), marks=78)

Of course you can cast TypedObject to target class you need (I recommend checking instance of before casting) and use:

Video video = (Video) toClazz(readObjects.get(0), objectMapper);
System.out.println(video.getVideoUrl());

I assumed you read whole collection once and you get all types mixed up in one list (as in example in your question). But you can try find documents in MongoDB by field "type" and get data separately for each of type. With this you can easily convert to each type separately.

like image 78
mkczyk Avatar answered Oct 06 '22 07:10

mkczyk