public String serialize()

in juneau-core/juneau-marshall/src/main/java/org/apache/juneau/oapi/OpenApiSerializerSession.java [259:432]


	public String serialize(HttpPartType partType, HttpPartSchema schema, Object value) throws SerializeException, SchemaValidationException {

		ClassMeta<?> type = getClassMetaForObject(value);
		if (type == null)
			type = object();

		// Swap if necessary
		ObjectSwap swap = type.getSwap(this);
		if (swap != null && ! type.isDateOrCalendarOrTemporal()) {
			value = swap(swap, value);
			type = swap.getSwapClassMeta(this);

			// If the getSwapClass() method returns Object, we need to figure out
			// the actual type now.
			if (type.isObject())
				type = getClassMetaForObject(value);
		}

		schema = ObjectUtils.firstNonNull(schema, DEFAULT_SCHEMA);

		HttpPartDataType t = schema.getType(type);

		HttpPartFormat f = schema.getFormat(type);
		if (f == HttpPartFormat.NO_FORMAT)
			f = ctx.getFormat();

		HttpPartCollectionFormat cf = schema.getCollectionFormat();
		if (cf == HttpPartCollectionFormat.NO_COLLECTION_FORMAT)
			cf = ctx.getCollectionFormat();

		String out = null;

		schema.validateOutput(value, ctx.getBeanContext());

		if (type.hasMutaterTo(schema.getParsedType()) || schema.getParsedType().hasMutaterFrom(type)) {
			value = toType(value, schema.getParsedType());
			type = schema.getParsedType();
		}

		if (type.isUri()) {
			value = getUriResolver().resolve(value);
			type = string();
		}

		if (value != null) {

			if (t == STRING) {

				if (f == BYTE) {
					out = base64Encode(toType(value, CM_ByteArray));
				} else if (f == BINARY) {
					out = toHex(toType(value, CM_ByteArray));
				} else if (f == BINARY_SPACED) {
					out = toSpacedHex(toType(value, CM_ByteArray));
				} else if (f == DATE) {
					try {
						if (value instanceof Calendar)
							out = TemporalCalendarSwap.IsoDate.DEFAULT.swap(this, (Calendar)value);
						else if (value instanceof Date)
							out = TemporalDateSwap.IsoDate.DEFAULT.swap(this, (Date)value);
						else if (value instanceof Temporal)
							out = TemporalSwap.IsoDate.DEFAULT.swap(this, (Temporal)value);
						else
							out = value.toString();
					} catch (Exception e) {
						throw new SerializeException(e);
					}
				} else if (f == DATE_TIME) {
					try {
						if (value instanceof Calendar)
							out = TemporalCalendarSwap.IsoInstant.DEFAULT.swap(this, (Calendar)value);
						else if (value instanceof Date)
							out = TemporalDateSwap.IsoInstant.DEFAULT.swap(this, (Date)value);
						else if (value instanceof Temporal)
							out = TemporalSwap.IsoInstant.DEFAULT.swap(this, (Temporal)value);
						else
							out = value.toString();
					} catch (Exception e) {
						throw new SerializeException(e);
					}
				} else if (f == HttpPartFormat.UON) {
					out = super.serialize(partType, schema, value);
				} else {
					out = toType(value, string());
				}

			} else if (t == BOOLEAN) {

				out = stringify(toType(value, CM_Boolean));

			} else if (t == INTEGER) {

				if (f == INT64)
					out = stringify(toType(value, CM_Long));
				else
					out = stringify(toType(value, CM_Integer));

			} else if (t == NUMBER) {

				if (f == DOUBLE)
					out = stringify(toType(value, CM_Double));
				else
					out = stringify(toType(value, CM_Float));

			} else if (t == ARRAY) {

				if (cf == HttpPartCollectionFormat.UONC)
					out = super.serialize(partType, null, toList(partType, type, value, schema));
				else {

					HttpPartSchema items = schema.getItems();
					ClassMeta<?> vt = getClassMetaForObject(value);
					OapiStringBuilder sb = new OapiStringBuilder(cf);

					if (type.isArray()) {
						for (int i = 0; i < Array.getLength(value); i++)
							sb.append(serialize(partType, items, Array.get(value, i)));
					} else if (type.isCollection()) {
						((Collection<?>)value).forEach(x -> sb.append(serialize(partType, items, x)));
					} else if (vt.hasMutaterTo(String[].class)) {
						String[] ss = toType(value, CM_StringArray);
						for (String element : ss)
                            sb.append(serialize(partType, items, element));
					} else {
						throw new SerializeException("Input is not a valid array type: " + type);
					}

					out = sb.toString();
				}

			} else if (t == OBJECT) {

				if (cf == HttpPartCollectionFormat.UONC) {
					if (schema.hasProperties() && type.isMapOrBean())
						value = toMap(partType, type, value, schema);
					out = super.serialize(partType, null, value);

				} else if (type.isBean()) {
					OapiStringBuilder sb = new OapiStringBuilder(cf);
					Predicate<Object> checkNull = x -> isKeepNullProperties() || x != null;
					HttpPartSchema schema2 = schema;

					toBeanMap(value).forEachValue(checkNull, (pMeta,key,val,thrown) -> {
						if (thrown == null)
							sb.append(key, serialize(partType, schema2.getProperty(key), val));
					});
					out = sb.toString();

				} else if (type.isMap()) {
					OapiStringBuilder sb = new OapiStringBuilder(cf);
					HttpPartSchema schema2 = schema;
					((Map<?,?>)value).forEach((k,v) -> sb.append(k, serialize(partType, schema2.getProperty(stringify(k)), v)));
					out = sb.toString();

				} else {
					throw new SerializeException("Input is not a valid object type: " + type);
				}

			} else if (t == FILE) {
				throw new SerializeException("File part not supported.");

			} else if (t == NO_TYPE) {
				// This should never be returned by HttpPartSchema.getType(ClassMeta).
				throw new SerializeException("Invalid type.");
			}
		}

		schema.validateInput(out);
		if (out == null)
			out = schema.getDefault();
		if (out == null)
			out = "null";
		return out;
	}