mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
remove DateValueWriter
This commit is contained in:
parent
7b7179b7fe
commit
27ff80d71f
@ -30,6 +30,7 @@ import org.dromara.hutool.core.reflect.TypeUtil;
|
||||
import org.dromara.hutool.core.text.CharUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.core.text.split.SplitUtil;
|
||||
import org.dromara.hutool.json.reader.JSONTokener;
|
||||
import org.dromara.hutool.json.serialize.GlobalSerializeMapping;
|
||||
import org.dromara.hutool.json.serialize.JSONDeserializer;
|
||||
import org.dromara.hutool.json.serialize.JSONStringer;
|
||||
|
@ -28,6 +28,11 @@ public class JSONObjectIter implements Iterable<JSONObject> {
|
||||
|
||||
Iterator<Object> iterator;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param iterator 迭代器
|
||||
*/
|
||||
public JSONObjectIter(final Iterator<Object> iterator) {
|
||||
this.iterator = iterator;
|
||||
}
|
||||
|
@ -30,6 +30,8 @@ import org.dromara.hutool.core.reflect.TypeUtil;
|
||||
import org.dromara.hutool.core.reflect.kotlin.KClassUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.json.*;
|
||||
import org.dromara.hutool.json.reader.JSONParser;
|
||||
import org.dromara.hutool.json.reader.JSONTokener;
|
||||
import org.dromara.hutool.json.serialize.JSONDeserializer;
|
||||
import org.dromara.hutool.json.serialize.JSONStringer;
|
||||
|
||||
|
@ -22,6 +22,8 @@ import org.dromara.hutool.core.lang.mutable.Mutable;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.core.array.ArrayUtil;
|
||||
import org.dromara.hutool.json.*;
|
||||
import org.dromara.hutool.json.reader.JSONParser;
|
||||
import org.dromara.hutool.json.reader.JSONTokener;
|
||||
import org.dromara.hutool.json.serialize.GlobalSerializeMapping;
|
||||
import org.dromara.hutool.json.serialize.JSONSerializer;
|
||||
|
||||
|
@ -25,6 +25,8 @@ import org.dromara.hutool.core.lang.mutable.MutableEntry;
|
||||
import org.dromara.hutool.core.reflect.method.MethodUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.json.*;
|
||||
import org.dromara.hutool.json.reader.JSONParser;
|
||||
import org.dromara.hutool.json.reader.JSONTokener;
|
||||
import org.dromara.hutool.json.serialize.GlobalSerializeMapping;
|
||||
import org.dromara.hutool.json.serialize.JSONSerializer;
|
||||
import org.dromara.hutool.json.xml.JSONXMLParser;
|
||||
|
@ -14,11 +14,12 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.dromara.hutool.json;
|
||||
package org.dromara.hutool.json.reader;
|
||||
|
||||
import org.dromara.hutool.core.lang.mutable.Mutable;
|
||||
import org.dromara.hutool.core.lang.mutable.MutableEntry;
|
||||
import org.dromara.hutool.core.text.CharUtil;
|
||||
import org.dromara.hutool.json.*;
|
||||
|
||||
import java.util.function.Predicate;
|
||||
|
||||
@ -163,10 +164,10 @@ public class JSONParser {
|
||||
for (; ; ) {
|
||||
if (x.nextClean() == CharUtil.COMMA) {
|
||||
x.back();
|
||||
jsonArray.addRaw(null, predicate);
|
||||
jsonArray.add(null, predicate);
|
||||
} else {
|
||||
x.back();
|
||||
jsonArray.addRaw(nextValue(), predicate);
|
||||
jsonArray.add(nextValue(), predicate);
|
||||
}
|
||||
switch (x.nextClean()) {
|
||||
case CharUtil.COMMA:
|
@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.dromara.hutool.json;
|
||||
package org.dromara.hutool.json.reader;
|
||||
|
||||
import org.dromara.hutool.core.io.IoUtil;
|
||||
import org.dromara.hutool.core.io.ReaderWrapper;
|
||||
@ -22,6 +22,7 @@ import org.dromara.hutool.core.lang.Assert;
|
||||
import org.dromara.hutool.core.math.NumberUtil;
|
||||
import org.dromara.hutool.core.text.CharUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.json.JSONException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
@ -0,0 +1,22 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Hutool Team and hutool.cn
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* JSON读取和解析
|
||||
*
|
||||
* @author Looly
|
||||
*/
|
||||
package org.dromara.hutool.json.reader;
|
@ -1,114 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2024 Hutool Team and hutool.cn
|
||||
*
|
||||
* 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.dromara.hutool.json.serialize;
|
||||
|
||||
import org.dromara.hutool.core.convert.ConvertUtil;
|
||||
import org.dromara.hutool.core.date.DateUtil;
|
||||
import org.dromara.hutool.core.date.TemporalAccessorUtil;
|
||||
import org.dromara.hutool.core.date.format.GlobalCustomFormat;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.json.InternalJSONUtil;
|
||||
import org.dromara.hutool.json.JSONConfig;
|
||||
|
||||
import java.time.MonthDay;
|
||||
import java.time.temporal.TemporalAccessor;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 自定义的日期表示形式<br>
|
||||
* 支持包括:{@link Date}、{@link Calendar}、{@link TemporalAccessor}
|
||||
*
|
||||
* @author looly
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public class DateJSONString implements JSONStringer {
|
||||
|
||||
final Object dateObj;
|
||||
final JSONConfig jsonConfig;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param dateObj 日期对象,支持包括Date、Calendar、TemporalAccessor
|
||||
* @param jsonConfig JSON配置
|
||||
*/
|
||||
public DateJSONString(final Object dateObj, final JSONConfig jsonConfig) {
|
||||
this.dateObj = dateObj;
|
||||
this.jsonConfig = jsonConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取原始的日期对象{@link Date}、{@link Calendar}、{@link TemporalAccessor}
|
||||
*
|
||||
* @return 日期对象
|
||||
*/
|
||||
@Override
|
||||
public Object getRaw() {
|
||||
return this.dateObj;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toJSONString() {
|
||||
// issue#2572@Github
|
||||
if (dateObj instanceof MonthDay) {
|
||||
return InternalJSONUtil.quote(dateObj.toString());
|
||||
}
|
||||
|
||||
return formatDate(this.dateObj, this.jsonConfig.getDateFormat());
|
||||
}
|
||||
|
||||
/**
|
||||
* 按照给定格式格式化日期,格式为空时返回时间戳字符串<br>
|
||||
* 如果给定的格式是时间戳,直接返回时间戳字符串,如果是给定字符串格式,返回带双引号包装的字符串
|
||||
*
|
||||
* @param dateObj Date或者Calendar对象
|
||||
* @param format 格式
|
||||
* @return 日期字符串
|
||||
*/
|
||||
private static String formatDate(final Object dateObj, final String format) {
|
||||
if (StrUtil.isNotBlank(format)) {
|
||||
final String dateStr;
|
||||
if (dateObj instanceof TemporalAccessor) {
|
||||
dateStr = TemporalAccessorUtil.format((TemporalAccessor) dateObj, format);
|
||||
} else {
|
||||
dateStr = DateUtil.format(ConvertUtil.toDate(dateObj), format);
|
||||
}
|
||||
|
||||
if (GlobalCustomFormat.FORMAT_SECONDS.equals(format)
|
||||
|| GlobalCustomFormat.FORMAT_MILLISECONDS.equals(format)) {
|
||||
// Hutool自定义的秒和毫秒表示,默认不包装双引号
|
||||
return dateStr;
|
||||
}
|
||||
//用户定义了日期格式
|
||||
return InternalJSONUtil.quote(dateStr);
|
||||
}
|
||||
|
||||
//默认使用时间戳
|
||||
final long timeMillis;
|
||||
if (dateObj instanceof TemporalAccessor) {
|
||||
timeMillis = TemporalAccessorUtil.toEpochMilli((TemporalAccessor) dateObj);
|
||||
} else if (dateObj instanceof Date) {
|
||||
timeMillis = ((Date) dateObj).getTime();
|
||||
} else if (dateObj instanceof Calendar) {
|
||||
timeMillis = ((Calendar) dateObj).getTimeInMillis();
|
||||
} else {
|
||||
throw new UnsupportedOperationException("Unsupported Date type: " + dateObj.getClass());
|
||||
}
|
||||
return String.valueOf(timeMillis);
|
||||
}
|
||||
}
|
@ -15,6 +15,17 @@
|
||||
*/
|
||||
|
||||
/**
|
||||
* JSON序列化和反序列化,提供对象和JSON之间的转换
|
||||
* JSON序列化和反序列化,提供对象和JSON之间的转换,我们定义:
|
||||
* 1. 对象定义:
|
||||
* <ul>
|
||||
* <li>Java对象,可以是POJO(Java Bean),也可以是String、int、Map,List等。</li>
|
||||
* <li>JSON对象,只包含JSON接口实现:JSONObject、JSONArray、JSONPrimitive和{@code null}。</li>
|
||||
* </ul>
|
||||
*
|
||||
* 2. 序列化定义:
|
||||
* <ul>
|
||||
* <li>序列化(Serialize) 指:【Java对象】 转换为 【JSON对象】</li>
|
||||
* <li>反序列化(Deserialize)指:【JSON对象】 转换为 【Java对象】</li>
|
||||
* </ul>
|
||||
*/
|
||||
package org.dromara.hutool.json.serialize;
|
||||
|
@ -16,8 +16,14 @@
|
||||
|
||||
package org.dromara.hutool.json.writer;
|
||||
|
||||
import org.dromara.hutool.json.serialize.DateJSONString;
|
||||
import org.dromara.hutool.core.convert.ConvertUtil;
|
||||
import org.dromara.hutool.core.date.DateUtil;
|
||||
import org.dromara.hutool.core.date.TemporalAccessorUtil;
|
||||
import org.dromara.hutool.core.date.format.GlobalCustomFormat;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.json.InternalJSONUtil;
|
||||
|
||||
import java.time.MonthDay;
|
||||
import java.time.temporal.TemporalAccessor;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
@ -42,6 +48,53 @@ public class DateValueWriter implements JSONValueWriter {
|
||||
|
||||
@Override
|
||||
public void write(final JSONWriter writer, final Object value) {
|
||||
writer.writeRaw(new DateJSONString(value, writer.getConfig()).toJSONString());
|
||||
final String rawString;
|
||||
// issue#2572@Github
|
||||
if (value instanceof MonthDay) {
|
||||
rawString = InternalJSONUtil.quote(value.toString());
|
||||
}else{
|
||||
rawString = formatDate(value, writer.getConfig().getDateFormat());
|
||||
}
|
||||
writer.writeRaw(rawString);
|
||||
}
|
||||
|
||||
/**
|
||||
* 按照给定格式格式化日期,格式为空时返回时间戳字符串<br>
|
||||
* 如果给定的格式是时间戳,直接返回时间戳字符串,如果是给定字符串格式,返回带双引号包装的字符串
|
||||
*
|
||||
* @param dateObj Date或者Calendar对象
|
||||
* @param format 格式
|
||||
* @return 日期字符串
|
||||
*/
|
||||
private static String formatDate(final Object dateObj, final String format) {
|
||||
if (StrUtil.isNotBlank(format)) {
|
||||
final String dateStr;
|
||||
if (dateObj instanceof TemporalAccessor) {
|
||||
dateStr = TemporalAccessorUtil.format((TemporalAccessor) dateObj, format);
|
||||
} else {
|
||||
dateStr = DateUtil.format(ConvertUtil.toDate(dateObj), format);
|
||||
}
|
||||
|
||||
if (GlobalCustomFormat.FORMAT_SECONDS.equals(format)
|
||||
|| GlobalCustomFormat.FORMAT_MILLISECONDS.equals(format)) {
|
||||
// Hutool自定义的秒和毫秒表示,默认不包装双引号
|
||||
return dateStr;
|
||||
}
|
||||
//用户定义了日期格式
|
||||
return InternalJSONUtil.quote(dateStr);
|
||||
}
|
||||
|
||||
//默认使用时间戳
|
||||
final long timeMillis;
|
||||
if (dateObj instanceof TemporalAccessor) {
|
||||
timeMillis = TemporalAccessorUtil.toEpochMilli((TemporalAccessor) dateObj);
|
||||
} else if (dateObj instanceof Date) {
|
||||
timeMillis = ((Date) dateObj).getTime();
|
||||
} else if (dateObj instanceof Calendar) {
|
||||
timeMillis = ((Calendar) dateObj).getTimeInMillis();
|
||||
} else {
|
||||
throw new UnsupportedOperationException("Unsupported Date type: " + dateObj.getClass());
|
||||
}
|
||||
return String.valueOf(timeMillis);
|
||||
}
|
||||
}
|
||||
|
@ -22,7 +22,8 @@ import java.util.*;
|
||||
|
||||
/**
|
||||
* 全局自定义对象写出<br>
|
||||
* 用户通过此全局定义,可针对某些特殊对象
|
||||
* 用户通过此全局定义,可针对某些特殊对象的写出<br>
|
||||
* 对象定义JSONValueWriter后,JSON中将存储原始对象
|
||||
*
|
||||
* @author looly
|
||||
* @since 6.0.0
|
||||
|
@ -18,9 +18,8 @@ package org.dromara.hutool.json.xml;
|
||||
|
||||
import org.dromara.hutool.core.text.CharUtil;
|
||||
import org.dromara.hutool.core.xml.XmlConstants;
|
||||
import org.dromara.hutool.json.JSONConfig;
|
||||
import org.dromara.hutool.json.JSONException;
|
||||
import org.dromara.hutool.json.JSONTokener;
|
||||
import org.dromara.hutool.json.reader.JSONTokener;
|
||||
|
||||
/**
|
||||
* XML分析器,继承自JSONTokener,提供XML的语法分析
|
||||
|
@ -18,6 +18,7 @@ package org.dromara.hutool.json;
|
||||
|
||||
import org.dromara.hutool.core.io.IoUtil;
|
||||
import org.dromara.hutool.core.io.resource.ResourceUtil;
|
||||
import org.dromara.hutool.json.reader.JSONTokener;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
Loading…
x
Reference in New Issue
Block a user