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