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

com.sap.cds.adapter.odata.v4.serializer.json.Struct2Json 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.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

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.String2Json;
import com.sap.cds.impl.parser.token.Jsonizer;

public abstract class Struct2Json implements Data2Json> {

	// prepared serializers and options:
	String2Json context;
	String2Json metadataEtag;
	String2Json> etag;
	String2Json type;
	List>> primitiveProperties;
	List>> navigationLinkProperties;
	List>> navigationProperties;
	// Contains a subtree for dynamic expands
	List>> dynamicProperties;
	// Set of dynamic properties that shouldn't be serialized
	Set excludedProperties;
	boolean isOpenType;

	boolean isRootLevel = true;
	boolean isAutoExpand = false;

	Struct2JsonBuilder builder;

	Struct2Json() {
	}

	public String2Json getContext() {
		return context;
	}

	public String2Json getMetadataEtag() {
		return metadataEtag;
	}

	protected void writeProperties(Map row, JsonGenerator json) throws IOException {
		// Collect all property names from data payload to be serialized
		Set rowProperties = isOpenType || isAutoExpand ? new HashSet<>(row.keySet()) : null;

		if (primitiveProperties != null) {
			for (Data2Json> p : primitiveProperties) {
				p.toJson(row, json);
				consumeProperty(rowProperties, p.getName());
			}
		}

		if (navigationLinkProperties != null) {
			for (Data2Json> n : navigationLinkProperties) {
				n.toJson(row, json);
			}
		}

		if (navigationProperties != null) {
			for (Data2Json> n : navigationProperties) {
				n.toJson(row, json);
				consumeProperty(rowProperties, n.getName());
			}
		}
		handleDynamicProperties(row, rowProperties, json);
	}

	private void handleDynamicProperties(Map row, Set properties, JsonGenerator json)
			throws IOException {
		if (isOpenType) {
			for (String p : properties) {
				Object pojo = row.get(p);
				json.writeFieldName(p);
				if (pojo == null) {
					json.writeNull();
				} else {
					json.writeRawValue(Jsonizer.json(pojo));
				}
			}
		} else if (properties != null) {
			// Lazy load navigation properties only, the rest (Draft_UUID, ...) is ignored
			if (excludedProperties != null) {
				properties.removeAll(excludedProperties);
			}
			if (properties.size() > 0 && builder != null) {
				builder.addDynamicExpandProperties(this, properties);
				for (Data2Json> n : dynamicProperties) {
					n.toJson(row, json);
				}
			}
		}
	}

	private void consumeProperty(Set rowProperties, String propertyName) {
		if (isAutoExpand) {
			rowProperties.remove(propertyName);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy