Persoenliche Mediazentrale
ulrich
2021-04-08 8d7d357497e80b87f1d3be2357cb9cb2e853e582
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package de.uhilger.mediaz.api;
 
import com.google.gson.Gson;
 
/**
 *
 * @author ulrich
 */
public class JsonHelper {
  
    public String jsonWithEnclosingType(Object o) {
    /*
    StringBuilder sb = new StringBuilder();
    sb.append("{\"");
    sb.append(o.getClass().getSimpleName());
    sb.append("\": ");
    Gson gson = new Gson();
    sb.append(gson.toJson(o));
    sb.append("}");
    return sb.toString();
    */
    return jsonWithCustomType(o, o.getClass().getSimpleName());
  }
  
  public String jsonWithCustomType(Object o, String typeName) {
    StringBuilder sb = new StringBuilder();
    sb.append("{\"");
    sb.append(typeName);
    sb.append("\": ");
    Gson gson = new Gson();
    sb.append(gson.toJson(o));
    sb.append("}");
    return sb.toString();
  }
  
}