Persoenliche Mediazentrale
ulrich
2021-04-24 94b1c2f60bde70d681b4bf8a5cd04b86e9ccf4ed
commit | author | age
8d7d35 1 /*
94b1c2 2   Tango - Personal Media Center
b56bb3 3   Copyright (C) 2021  Ulrich Hilger
U 4
5   This program is free software: you can redistribute it and/or modify
6   it under the terms of the GNU Affero General Public License as
7   published by the Free Software Foundation, either version 3 of the
8   License, or (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU Affero General Public License for more details.
14
15   You should have received a copy of the GNU Affero General Public License
16   along with this program.  If not, see <https://www.gnu.org/licenses/>.
8d7d35 17  */
94b1c2 18 package de.uhilger.tango.api;
8d7d35 19
U 20 import com.google.gson.Gson;
21
22 /**
23  *
b56bb3 24  * @author Ulrich Hilger
U 25  * @version 1, 9.4.2021
8d7d35 26  */
U 27 public class JsonHelper {
28   
29     public String jsonWithEnclosingType(Object o) {
30     /*
31     StringBuilder sb = new StringBuilder();
32     sb.append("{\"");
33     sb.append(o.getClass().getSimpleName());
34     sb.append("\": ");
35     Gson gson = new Gson();
36     sb.append(gson.toJson(o));
37     sb.append("}");
38     return sb.toString();
39     */
40     return jsonWithCustomType(o, o.getClass().getSimpleName());
41   }
42   
43   public String jsonWithCustomType(Object o, String typeName) {
fcc734 44     /*
8d7d35 45     StringBuilder sb = new StringBuilder();
U 46     sb.append("{\"");
47     sb.append(typeName);
48     sb.append("\": ");
49     Gson gson = new Gson();
50     sb.append(gson.toJson(o));
51     sb.append("}");
52     return sb.toString();
fcc734 53     */
U 54     Gson gson = new Gson();
55     return embedInCustomType(gson.toJson(o), typeName);
56   }
57   
58   public String embedInCustomType(String jsonStr, String typeName) {
59     StringBuilder sb = new StringBuilder();
60     sb.append("{\"");
61     sb.append(typeName);
62     sb.append("\": ");
63     //Gson gson = new Gson();
64     //sb.append(gson.toJson(o));
65     sb.append(jsonStr);
66     sb.append("}");
67     return sb.toString();
8d7d35 68   }
U 69   
70 }