All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.yaml.snakeyaml.representer.JsonRepresenter Maven / Gradle / Ivy

Go to download

Easy Redis Java client and Real-Time Data Platform. Valkey compatible. Sync/Async/RxJava3/Reactive API. Client side caching. Over 50 Redis based Java objects and services: JCache API, Apache Tomcat, Hibernate, Spring, Set, Multimap, SortedSet, Map, List, Queue, Deque, Semaphore, Lock, AtomicLong, Map Reduce, Bloom filter, Scheduler, RPC

There is a newer version: 3.43.0
Show newest version
/**
 * Copyright (c) 2008, SnakeYAML
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package org.yaml.snakeyaml.representer;

import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.nodes.Tag;

import java.util.Date;

/**
 * Represent data structures into Nodes in the way suitable for serialisation as JSON
 */
public class JsonRepresenter extends Representer {
  public JsonRepresenter(DumperOptions options) {
    super(options);
    this.representers.put(byte[].class, new RepresentByteArray());
    this.multiRepresenters.put(Date.class, new RepresentDate());
    if (options.getDefaultScalarStyle() != DumperOptions.ScalarStyle.JSON_SCALAR_STYLE) {
      throw new IllegalStateException("JSON requires ScalarStyle.JSON_SCALAR_STYLE");
    }
    if (options.getNonPrintableStyle() != DumperOptions.NonPrintableStyle.ESCAPE) {
      throw new IllegalStateException("JSON requires NonPrintableStyle.ESCAPE");
    }
  }

  /**
   * Use the provided serialisation but emit as string
   */
  protected class RepresentDate extends Representer.RepresentDate {
    @Override
    public Tag getDefaultTag() {
      return Tag.STR;
    }
  }

  /**
   * JSON does not have support for binary data. This method should be overridden to emit the
   * expected string for binary data
   */
  protected class RepresentByteArray implements Represent {
    public Node representData(Object data) {
      char[] binary = Base64Coder.encode((byte[]) data);
      return representScalar(Tag.STR, String.valueOf(binary));
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy