Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
// Copyright (c) Keith D Gregory
//
// 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 com.kdgregory.logging.common.util;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Map;
import java.util.TimeZone;
/**
* Transforms a Map into a JSON string. Transforms strings, numbers, booleans,
* and null as expected; maps become nested objects; arrays and collections
* (list, set, whatever) become arrays; dates become strings formatted as
* ISO-8601 timestamps; anything else is converted to a string.
*
* Instances are not thread-safe
*/
public class JsonConverter
{
public String convert(Map map)
{
StringBuilder builder = new StringBuilder(1024);
appendMap(builder, map);
return builder.toString();
}
private void append(StringBuilder builder, String key, Object value)
{
appendString(builder, key);
builder.append(':');
appendValue(builder, value);
}
private void appendValue(StringBuilder builder, Object value)
{
if (value instanceof String) appendString(builder, (String)value);
else if (value instanceof Number) appendNumber(builder, (Number)value);
else if (value instanceof Boolean) appendBoolean(builder, (Boolean)value);
else if (value instanceof Date) appendDate(builder, (Date)value);
else if (value instanceof Object[]) appendArray(builder, (Object[])value);
else if (value instanceof Collection) appendCollection(builder, (Collection