com.liferay.bulk.rest.client.serdes.v1_0.SelectionSerDes Maven / Gradle / Ivy
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.bulk.rest.client.serdes.v1_0;
import com.liferay.bulk.rest.client.dto.v1_0.Selection;
import com.liferay.bulk.rest.client.json.BaseJSONParser;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeMap;
import javax.annotation.Generated;
/**
* @author Alejandro Tardín
* @generated
*/
@Generated("")
public class SelectionSerDes {
public static Selection toDTO(String json) {
SelectionJSONParser selectionJSONParser = new SelectionJSONParser();
return selectionJSONParser.parseToDTO(json);
}
public static Selection[] toDTOs(String json) {
SelectionJSONParser selectionJSONParser = new SelectionJSONParser();
return selectionJSONParser.parseToDTOs(json);
}
public static String toJSON(Selection selection) {
if (selection == null) {
return "null";
}
StringBuilder sb = new StringBuilder();
sb.append("{");
if (selection.getSize() != null) {
if (sb.length() > 1) {
sb.append(", ");
}
sb.append("\"size\": ");
sb.append(selection.getSize());
}
sb.append("}");
return sb.toString();
}
public static Map toMap(String json) {
SelectionJSONParser selectionJSONParser = new SelectionJSONParser();
return selectionJSONParser.parseToMap(json);
}
public static Map toMap(Selection selection) {
if (selection == null) {
return null;
}
Map map = new TreeMap<>();
if (selection.getSize() == null) {
map.put("size", null);
}
else {
map.put("size", String.valueOf(selection.getSize()));
}
return map;
}
public static class SelectionJSONParser extends BaseJSONParser {
@Override
protected Selection createDTO() {
return new Selection();
}
@Override
protected Selection[] createDTOArray(int size) {
return new Selection[size];
}
@Override
protected void setField(
Selection selection, String jsonParserFieldName,
Object jsonParserFieldValue) {
if (Objects.equals(jsonParserFieldName, "size")) {
if (jsonParserFieldValue != null) {
selection.setSize(
Long.valueOf((String)jsonParserFieldValue));
}
}
else {
throw new IllegalArgumentException(
"Unsupported field name " + jsonParserFieldName);
}
}
}
private static String _escape(Object object) {
String string = String.valueOf(object);
for (String[] strings : BaseJSONParser.JSON_ESCAPE_STRINGS) {
string = string.replace(strings[0], strings[1]);
}
return string;
}
private static String _toJSON(Map map) {
StringBuilder sb = new StringBuilder("{");
@SuppressWarnings("unchecked")
Set set = map.entrySet();
@SuppressWarnings("unchecked")
Iterator> iterator = set.iterator();
while (iterator.hasNext()) {
Map.Entry entry = iterator.next();
sb.append("\"");
sb.append(entry.getKey());
sb.append("\":");
Object value = entry.getValue();
Class> valueClass = value.getClass();
if (value instanceof Map) {
sb.append(_toJSON((Map)value));
}
else if (valueClass.isArray()) {
Object[] values = (Object[])value;
sb.append("[");
for (int i = 0; i < values.length; i++) {
sb.append("\"");
sb.append(_escape(values[i]));
sb.append("\"");
if ((i + 1) < values.length) {
sb.append(", ");
}
}
sb.append("]");
}
else {
sb.append("\"");
sb.append(_escape(entry.getValue()));
sb.append("\"");
}
if (iterator.hasNext()) {
sb.append(",");
}
}
sb.append("}");
return sb.toString();
}
}