44import com .contentful .java .cma .model .CMASystem ;
55import com .contentful .java .cma .model .CMATag ;
66import com .contentful .java .cma .model .CMAType ;
7+ import com .contentful .java .cma .model .CMALink ;
78import com .google .gson .JsonArray ;
89import com .google .gson .JsonElement ;
910import com .google .gson .JsonObject ;
@@ -17,23 +18,56 @@ public class MetadataSerializer implements JsonSerializer<CMAMetadata> {
1718 @ Override
1819 public JsonElement serialize (CMAMetadata src , Type typeOfSrc , JsonSerializationContext ctx ) {
1920 final JsonObject result = new JsonObject ();
21+
22+ // Always include tags field, even if empty
2023 result .add ("tags" , serializeTags (src .getTags ()));
24+
25+ // Serialize concepts if present
26+ if (src .getConcepts () != null && !src .getConcepts ().isEmpty ()) {
27+ result .add ("concepts" , serializeLinks (
28+ src .getConcepts (), CMAType .TaxonomyConcept ));
29+ }
30+
31+ // Serialize taxonomies if present
32+ if (src .getTaxonomy () != null && !src .getTaxonomy ().isEmpty ()) {
33+ result .add ("taxonomy" , serializeLinks (
34+ src .getTaxonomy (), CMAType .TaxonomyConceptScheme ));
35+ }
36+
2137 return result ;
2238 }
2339
2440 private JsonArray serializeTags (List <CMATag > tags ) {
2541 JsonArray jsonArray = new JsonArray ();
26- for (CMATag tag : tags ) {
27- JsonObject tagObject = new JsonObject ();
42+ if (tags != null ) {
43+ for (CMATag tag : tags ) {
44+ JsonObject tagObject = new JsonObject ();
45+ JsonObject sysObject = new JsonObject ();
46+ CMASystem system = tag .getSystem ();
47+
48+ sysObject .addProperty ("type" , CMAType .Link .toString ());
49+ sysObject .addProperty ("linkType" , CMAType .Tag .toString ());
50+ sysObject .addProperty ("id" , system .getId ());
51+
52+ tagObject .add ("sys" , sysObject );
53+ jsonArray .add (tagObject );
54+ }
55+ }
56+ return jsonArray ;
57+ }
58+
59+ private JsonArray serializeLinks (List <CMALink > links , CMAType linkType ) {
60+ JsonArray jsonArray = new JsonArray ();
61+ for (CMALink link : links ) {
62+ JsonObject linkObject = new JsonObject ();
2863 JsonObject sysObject = new JsonObject ();
29- CMASystem system = tag .getSystem ();
3064
3165 sysObject .addProperty ("type" , CMAType .Link .toString ());
32- sysObject .addProperty ("linkType" , CMAType . Tag .toString ());
33- sysObject .addProperty ("id" , system .getId ());
66+ sysObject .addProperty ("linkType" , linkType .toString ());
67+ sysObject .addProperty ("id" , link .getId ());
3468
35- tagObject .add ("sys" , sysObject );
36- jsonArray .add (tagObject );
69+ linkObject .add ("sys" , sysObject );
70+ jsonArray .add (linkObject );
3771 }
3872 return jsonArray ;
3973 }
0 commit comments