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 static org.apache.juneau.httppart.HttpPartType.*;
016
017import org.apache.juneau.examples.core.pojo.*;
018import org.apache.juneau.httppart.*;
019import org.apache.juneau.oapi.*;
020
021/**
022 * Sample class which shows the simple usage of OpenApiSerializer.
023 *
024 * <h5 class='section'>See Also:</h5><ul>
025 * </ul>
026 */
027public class OapiExample {
028
029
030   /**
031    * Get a reference to a parser and usage of oapiserializer.
032    *
033    * @param args Unused.
034    * @throws Exception Unused.
035    */
036   @SuppressWarnings("unused")
037   public static void main(String[] args) throws Exception{
038
039      OpenApiSerializer oapiSerializer = OpenApiSerializer.DEFAULT;
040
041      OpenApiParser oapiParser = OpenApiParser.DEFAULT;
042
043      Pojo pojo = new Pojo("id","name");
044
045      String flat = oapiSerializer.serialize(pojo);
046      // Print out the created POJO in OpenAPI format.
047
048      Pojo parse = oapiParser.parse(flat, Pojo.class);
049
050      assert parse.getId().equals(pojo.getId());
051      assert parse.getName().equals(pojo.getName());
052
053      //Http part schmea
054      HttpPartSchema schema = HttpPartSchema
055         .create("array")
056         .collectionFormat("pipes")
057         .items(
058            HttpPartSchema
059            .create("array")
060            .collectionFormat("csv")
061            .items(
062               HttpPartSchema.create("integer","int64")
063            )
064         )
065         .build();
066      Object value = new long[][]{{1,2,3},{4,5,6},{7,8,9}};
067      String output = OpenApiSerializer.DEFAULT.serialize(HEADER, schema, value);
068
069      HttpPartSchema schemab = HttpPartSchema.create().type("string").build();
070      // Convert POJO to BASE64-encoded string.
071      HttpPartSerializer s = OpenApiSerializer.DEFAULT;
072      String httpPart = s.getPartSession().serialize(HEADER, schemab, pojo);
073      System.out.println(httpPart);
074
075      // Convert BASE64-encoded string back into a POJO.
076      HttpPartParser p = OpenApiParser.DEFAULT;
077      pojo = p.getPartSession().parse(HEADER, schemab, httpPart, oapiParser.getClassMeta(Pojo.class));
078
079      // The object above can be parsed thanks to the @Beanc(properties = id,name) annotation on Pojo
080      // Using this approach, you can keep your POJOs immutable, and still serialize and deserialize them.
081   }
082}