001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.examples.core.oapi;
014
015import org.apache.juneau.examples.core.pojo.Pojo;
016import org.apache.juneau.httppart.HttpPartParser;
017import org.apache.juneau.httppart.HttpPartSchema;
018import org.apache.juneau.httppart.HttpPartSerializer;
019import org.apache.juneau.httppart.HttpPartType;
020import org.apache.juneau.oapi.OpenApiParser;
021import org.apache.juneau.oapi.OpenApiSerializer;
022
023/**
024 * Sample class which shows the simple usage of OpenApiSerializer
025 */
026public class OapiExample {
027
028
029    /**
030     * Get a reference to a parser and usage of oapiserializer.
031     * @param args
032     * @throws Exception
033     */
034    @SuppressWarnings("unused")
035   public static void main(String[] args) throws Exception{
036
037        OpenApiSerializer oapiSerializer = OpenApiSerializer.DEFAULT;
038
039        OpenApiParser oapiParser = OpenApiParser.DEFAULT;
040
041        Pojo pojo = new Pojo("id","name");
042
043        String flat = oapiSerializer.serialize(pojo);
044        // Print out the created POJO in OpenAPI format.
045
046        Pojo parse = oapiParser.parse(flat, Pojo.class);
047
048        assert parse.getId().equals(pojo.getId());
049        assert parse.getName().equals(pojo.getName());
050
051        //Http part schmea
052        HttpPartSchema schema = HttpPartSchema
053                .create("array")
054                .collectionFormat("pipes")
055                .items(
056                        HttpPartSchema
057                                .create("array")
058                                .collectionFormat("csv")
059                                .items(
060                                        HttpPartSchema.create("integer","int64")
061                                )
062                )
063                .build();
064        Object value = new long[][]{{1,2,3},{4,5,6},{7,8,9}};
065        String output = OpenApiSerializer.DEFAULT.serialize(HttpPartType.HEADER, schema, value);
066
067        HttpPartSchema schemab = HttpPartSchema.create().type("string").build();
068        // Convert POJO to BASE64-encoded string.
069        HttpPartSerializer s = OpenApiSerializer.DEFAULT;
070        String httpPart = s.serialize(schemab, pojo);
071        System.out.println(httpPart);
072
073        // Convert BASE64-encoded string back into a POJO.
074        HttpPartParser p = OpenApiParser.DEFAULT;
075        pojo = p.parse(schemab, httpPart, Pojo.class);
076
077        // The object above can be parsed thanks to the @BeanConstructor(properties = id,name) annotation on Pojo
078        // Using this approach, you can keep your POJOs immutable, and still serialize and deserialize them.
079    }
080}