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