All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sap.cds.adapter.odata.v4.serializer.json.StructCollection2Json Maven / Gradle / Ivy

There is a newer version: 3.2.0
Show newest version
/**************************************************************************
 * (C) 2019-2024 SAP SE or an SAP affiliate company. All rights reserved. *
 **************************************************************************/
package com.sap.cds.adapter.odata.v4.serializer.json;

import java.io.IOException;
import java.util.Map;

import org.apache.olingo.commons.api.Constants;

import com.fasterxml.jackson.core.JsonGenerator;
import com.sap.cds.adapter.odata.v4.serializer.json.api.Data2Json;
import com.sap.cds.adapter.odata.v4.serializer.json.primitive.Number2Json;
import com.sap.cds.adapter.odata.v4.serializer.json.primitive.String2Json;

public abstract class StructCollection2Json implements Data2Json>> {

	// prepared serializers:
	Struct2Json entity2Json;
	String2Json nextLink;
	Number2Json>> count;
	String2Json>> countStr;
	String navigationPropertyName;
	boolean writeOnlyReferences;

	StructCollection2Json() {
	}

	@Override
	public void toJson(Iterable> maps, JsonGenerator json) throws IOException {
		if (navigationPropertyName == null) {
			json.writeStartObject();
			writeMetadata(maps, json);
			json.writeFieldName(Constants.VALUE);
			writeJsonArray(maps, json);
			// operations go here

			if (nextLink!= null) {
				nextLink.toJson(null, json);
			}
			// delta link goes here

			json.writeEndObject();
		} else {
			writeMetadata(maps, json);
			json.writeFieldName(navigationPropertyName);
			writeJsonArray(maps, json);
		}
	}

	private void writeMetadata(Iterable> rows, JsonGenerator json) throws IOException {

		// @context
		if (entity2Json.getContext() != null) {
			entity2Json.getContext().toJson(null, json);
		}

		// @etag
		if (entity2Json.getMetadataEtag() != null) {
			entity2Json.getMetadataEtag().toJson(null, json);
		}

		// @count
		if (count != null) {
			count.toJson(rows, json);
		} else if (countStr != null) {
			countStr.toJson(rows, json);
		}
	}

	protected abstract void writeJsonArray(Iterable> rows, JsonGenerator json) throws IOException;
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy