lundi 5 septembre 2016

Build nested Json with Reflection Field in java

I want to generate Nested Json with reflection and Annotation My Class is :

@FormBuilderEnum(Validation = true)
private ReifenWechslerTypDto typ;

@FormBuilderObject()
private List<ReifenWechslerAvailabiltyDto> availabilties;

@FormBuilderObject()
private AdresseDto adresse;

@FormBuilderFieldType(Datatype = "label")
private static final long serialVersionUID = 1L;



@FormBuilderObject()
private KontaktDatenDto kontakt;

@FormBuilderObject()
private RegionDto region;

and my AdresseDto is :

@FormBuilderFieldType()
private String anrede;
@FormBuilderFieldType()
private String firma;
@FormBuilderFieldType(Datatype = "textarea")
private String vorNach;
@FormBuilderFieldType()
private String strasse;
@FormBuilderFieldType(Datatype = "textarea")
private String hausNr;
@FormBuilderFieldType(Datatype = "password")
private String zusatz;
@FormBuilderObject()
private MinimalOrtDto ort;
@FormBuilderObject()
private GPSPositionDto gpsPos;

I want to Use Reflection to generate Nested json as :

[
 {
   "id": 100,
   "adresse": {
  "id": 100,
  "anrede": null,
  "firma": null,
  "vorNach": null,
  "strasse": "Miraustraße",
  "hausNr": "38",
  "zusatz": null,
  "ort": {
    "id": 9341,
    "land": {
      "name": "Deutschland",
      "autoKz": "DE",
      "alpha2": "DE",
      "alpha3": "DEU",
      "tld": "de",
      "id": 1
    },
    "Kontakt": {
      "id": 3,
      "name": "Berlin "
    }]

There is 2 Annotation : 1-FormBuilderObject 2-FormBuilderFieldType

with FormBuilderObject must generate new json and FormBuilderFieldType is child for Object.

My reflection code is :

    public static List<FormMetaModel> fields = new ArrayList<>();
   public static JSONArray globalJsonArrays = new JSONArray();

  String parent = "";


JSONObject mainObj = new JSONObject();
  @Override
public JSONObject getEntities(String entitiesName) {
    Reflections reflections = new Reflections("de.wits.rwd.dto");
    Map<String, Class<?>> classBySimpleName = new HashMap<>();
    Set<Class<?>> annotatedPlugin =      reflections.getTypesAnnotatedWith(FormBuliderClassType.class);
    Class<?> clazz = null;
    for (Class cla : annotatedPlugin) {
        FormBuliderClassType pluginAnnotation = (FormBuliderClassType)    cla.getAnnotation(FormBuliderClassType.class);
        classBySimpleName.put(cla.getSimpleName(), cla);
        if (pluginAnnotation.entitieName().equals(entitiesName)) {
            clazz = cla;
            break;
        } else if (cla.getSimpleName().equals(entitiesName)) {
            clazz = cla;
            break;
        }
    }
    if (clazz == null) {
        throw new FourWitsRuntimeException("Entities Nicht gefunden");
    }
    Field f[] = clazz.getDeclaredFields();
    for (int i = 0; i < f.length; i++) {
        addToList(f[i]);
    }
    return mainObj;
}

and :

 public void addToList(Field field) {
        Annotation[] fieldAnnotation = field.getAnnotations();
        for (Annotation annotation : fieldAnnotation) {
            if (annotation instanceof FormBuilderObject) {
                parent = field.getName();
                Type type = field.getGenericType();
                if (type instanceof ParameterizedType) {
                    ParameterizedType pType = (ParameterizedType) type;
                    Type[] arr = pType.getActualTypeArguments();
                    for (Type tp : arr) {
                        Class<?> clzz = (Class<?>) tp;
                        Field[] allFields = clzz.getDeclaredFields();
                        for (Field childField : allFields) {
                            addToList(childField);
                        }
                    }
                } else {
                    Field[] allFields = field.getType().getDeclaredFields();
                    for (Field childField : allFields) {
                        addToList(childField);
                    }
                }
                mainObj.put(field.getName(),"");
            } else if (annotation instanceof FormBuilderFieldType) {
                FormMetaModel fieldObj = new FormMetaModel();
                JSONArray jsonArray = new JSONArray();
                JSONObject jsonObject = new JSONObject();
                FormBuilderFieldType myAnnotation = (FormBuilderFieldType) annotation;
                jsonObject.put("Type",myAnnotation.Datatype());
                jsonObject.put("Required",myAnnotation.Datatype());
                jsonObject.put("Name",myAnnotation.Datatype());
                jsonObject.put("DataType",myAnnotation.Datatype());
                jsonObject.put("Value",myAnnotation.Datatype());
                jsonArray.put(jsonObject);

                globalJsonArrays.put(jsonArray);

            }
            else if (annotation instanceof FormBuilderEnum) {
                FormMetaModel fieldObj = new FormMetaModel();
                JSONArray jsonArray = new JSONArray();
                JSONObject jsonObject = new JSONObject();
                FormBuilderEnum myEnumAnnotation = (FormBuilderEnum) annotation;
                fieldObj.setRequired(myEnumAnnotation.Validation());
                Field[] enumFields = field.getType().getDeclaredFields();
                List<String> values = new ArrayList<>();
                for(Field childEnumField : enumFields){
                    values.add(childEnumField.getName());
                }
                values.remove(enumFields.length - 1);

                jsonObject.put("Type","enum");
                jsonObject.put("Options",values);
                jsonObject.put("Name",field.getName());
                jsonObject.put("DataType",field.getType().toString());
                jsonObject.put("Value",field.getName());
                jsonArray.put(jsonObject);


                globalJsonArrays.put(jsonArray);
            }
            mainObj.put(parent,globalJsonArrays);

    }

how can I generate a nested Json as a Sample in Java with reflection?





Aucun commentaire:

Enregistrer un commentaire