浏览代码

fix code and add sh

Looly 5 年之前
父节点
当前提交
ccccbf67aa

+ 2 - 1
CHANGELOG.md

@@ -3,12 +3,13 @@
 
 -------------------------------------------------------------------------------------------------------------
 
-## 5.2.6 (2020-03-26)
+## 5.2.6 (2020-04-01)
 
 ### 新特性
 * 【extra  】     JschUtil增加execByShell方法(issue#I1CYES@Gitee)
 * 【core   】     StrUtil增加subBetweenAll方法,Console增加where和lineNumber方法(issue#812@Github)
 * 【core   】     TableMap增加getKeys和getValues方法
+* 【json   】     JSONObject和JSONArray增加set方法,标识put弃用
 
 ### Bug修复
 * 【extra  】     修复SpringUtil使用devtools重启报错问题

+ 3 - 0
bin/cobertura.sh

@@ -0,0 +1,3 @@
+#!/bin/bash
+
+exec mvn -T 1 cobertura:cobertura

+ 3 - 0
bin/simple_install.sh

@@ -0,0 +1,3 @@
+#!/bin/bash
+
+exec mvn -T 1C clean install -Dmaven.test.skip=true -Dmaven.javadoc.skip=true

+ 15 - 2
hutool-json/src/main/java/cn/hutool/json/JSONArray.java

@@ -252,8 +252,21 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
 	 *
 	 * @param value 值,可以是: Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the JSONNull.NULL。
 	 * @return this.
+	 * @see #set(Object)
 	 */
 	public JSONArray put(Object value) {
+		return set(value);
+	}
+
+	/**
+	 * Append an object value. This increases the array's length by one. <br>
+	 * 加入元素,数组长度+1,等同于 {@link JSONArray#add(Object)}
+	 *
+	 * @param value 值,可以是: Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the JSONNull.NULL。
+	 * @return this.
+	 * @since 5.2.5
+	 */
+	public JSONArray set(Object value) {
 		this.add(value);
 		return this;
 	}
@@ -284,7 +297,7 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
 		}
 		JSONObject jo = new JSONObject();
 		for (int i = 0; i < names.size(); i += 1) {
-			jo.put(names.getStr(i), this.getObj(i));
+			jo.set(names.getStr(i), this.getObj(i));
 		}
 		return jo;
 	}
@@ -440,7 +453,7 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
 			while (index != this.size()) {
 				this.add(JSONNull.NULL);
 			}
-			this.put(element);
+			this.set(element);
 		}
 
 	}

+ 23 - 8
hutool-json/src/main/java/cn/hutool/json/JSONObject.java

@@ -279,7 +279,7 @@ public class JSONObject implements JSON, JSONGetter<String>, Map<String, Object>
 		for (String name : names) {
 			value = this.get(name);
 			if (null != value) {
-				ja.put(value);
+				ja.set(value);
 			}
 		}
 		return ja;
@@ -332,14 +332,16 @@ public class JSONObject implements JSON, JSONGetter<String>, Map<String, Object>
 	}
 
 	/**
-	 * PUT 键值对到JSONObject中,如果值为<code>null</code>,将此键移除
+	 * PUT 键值对到JSONObject中,在忽略null模式下,如果值为<code>null</code>,将此键移除
 	 *
 	 * @param key 键
 	 * @param value 值对象. 可以是以下类型: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull.NULL.
 	 * @return this.
 	 * @throws JSONException 值是无穷数字抛出此异常
+	 * @deprecated 此方法存在歧义,原Map接口返回的是之前的值,重写后返回this了,未来版本此方法会修改,请使用{@link #set(String, Object)}
 	 */
 	@Override
+	@Deprecated
 	public JSONObject put(String key, Object value) throws JSONException {
 		if (null == key) {
 			return this;
@@ -357,6 +359,19 @@ public class JSONObject implements JSON, JSONGetter<String>, Map<String, Object>
 	}
 
 	/**
+	 * 设置键值对到JSONObject中,在忽略null模式下,如果值为<code>null</code>,将此键移除
+	 *
+	 * @param key 键
+	 * @param value 值对象. 可以是以下类型: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull.NULL.
+	 * @return this.
+	 * @throws JSONException 值是无穷数字抛出此异常
+	 */
+	public JSONObject set(String key, Object value) throws JSONException {
+		put(key, value);
+		return this;
+	}
+
+	/**
 	 * 一次性Put 键值对,如果key已经存在抛出异常,如果键值中有null值,忽略
 	 *
 	 * @param key 键
@@ -365,7 +380,7 @@ public class JSONObject implements JSON, JSONGetter<String>, Map<String, Object>
 	 * @throws JSONException 值是无穷数字、键重复抛出异常
 	 */
 	public JSONObject putOnce(String key, Object value) throws JSONException {
-		if (key != null && value != null) {
+		if (key != null) {
 			if (rawHashMap.containsKey(key)) {
 				throw new JSONException("Duplicate key \"{}\"", key);
 			}
@@ -409,11 +424,11 @@ public class JSONObject implements JSON, JSONGetter<String>, Map<String, Object>
 		InternalJSONUtil.testValidity(value);
 		Object object = this.getObj(key);
 		if (object == null) {
-			this.put(key, value instanceof JSONArray ? new JSONArray().put(value) : value);
+			this.put(key, value instanceof JSONArray ? new JSONArray().set(value) : value);
 		} else if (object instanceof JSONArray) {
-			((JSONArray) object).put(value);
+			((JSONArray) object).set(value);
 		} else {
-			this.put(key, new JSONArray().put(object).put(value));
+			this.set(key, new JSONArray().set(object).set(value));
 		}
 		return this;
 	}
@@ -430,9 +445,9 @@ public class JSONObject implements JSON, JSONGetter<String>, Map<String, Object>
 		InternalJSONUtil.testValidity(value);
 		Object object = this.getObj(key);
 		if (object == null) {
-			this.put(key, new JSONArray().put(value));
+			this.set(key, new JSONArray().set(value));
 		} else if (object instanceof JSONArray) {
-			this.put(key, ((JSONArray) object).put(value));
+			this.set(key, ((JSONArray) object).set(value));
 		} else {
 			throw new JSONException("JSONObject [" + key + "] is not a JSONArray.");
 		}

+ 6 - 6
hutool-json/src/main/java/cn/hutool/json/JSONObjectIter.java

@@ -10,10 +10,10 @@ import java.util.Iterator;
  */
 public class JSONObjectIter implements Iterable<JSONObject> {
 
-	Iterator<Object> iter;
+	Iterator<Object> iterator;
 	
-	public JSONObjectIter(Iterator<Object> iter) {
-		this.iter = iter;
+	public JSONObjectIter(Iterator<Object> iterator) {
+		this.iterator = iterator;
 	}
 
 	@Override
@@ -22,17 +22,17 @@ public class JSONObjectIter implements Iterable<JSONObject> {
 
 			@Override
 			public boolean hasNext() {
-				return iter.hasNext();
+				return iterator.hasNext();
 			}
 
 			@Override
 			public JSONObject next() {
-				return (JSONObject) iter.next();
+				return (JSONObject) iterator.next();
 			}
 
 			@Override
 			public void remove() {
-				iter.remove();
+				iterator.remove();
 			}
 		};
 	}

+ 46 - 44
hutool-json/src/test/java/cn/hutool/json/JSONObjectTest.java

@@ -1,17 +1,7 @@
 package cn.hutool.json;
 
-import java.math.BigDecimal;
-import java.util.Date;
-import java.util.List;
-import java.util.Objects;
-
 import cn.hutool.core.annotation.Alias;
 import cn.hutool.core.collection.CollUtil;
-import lombok.Data;
-import org.junit.Assert;
-import org.junit.Ignore;
-import org.junit.Test;
-
 import cn.hutool.core.collection.CollectionUtil;
 import cn.hutool.core.date.DatePattern;
 import cn.hutool.core.date.DateUtil;
@@ -30,6 +20,15 @@ import cn.hutool.json.test.bean.UserWithMap;
 import cn.hutool.json.test.bean.report.CaseReport;
 import cn.hutool.json.test.bean.report.StepReport;
 import cn.hutool.json.test.bean.report.SuiteReport;
+import lombok.Data;
+import org.junit.Assert;
+import org.junit.Ignore;
+import org.junit.Test;
+
+import java.math.BigDecimal;
+import java.util.Date;
+import java.util.List;
+import java.util.Objects;
 
 /**
  * JSONObject单元测试
@@ -61,32 +60,33 @@ public class JSONObjectTest {
 	@Test
 	public void toStringTest3() {
 		JSONObject json = Objects.requireNonNull(JSONUtil.createObj()//
-				.put("dateTime", DateUtil.parse("2019-05-02 22:12:01")))//
+				.set("dateTime", DateUtil.parse("2019-05-02 22:12:01")))//
 				.setDateFormat(DatePattern.NORM_DATE_PATTERN);
 		Assert.assertEquals("{\"dateTime\":\"2019-05-02\"}", json.toString());
 	}
 
 	@Test
 	public void toStringWithDateTest() {
-		JSONObject json = JSONUtil.createObj().put("date", DateUtil.parse("2019-05-08 19:18:21"));
+		JSONObject json = JSONUtil.createObj().set("date", DateUtil.parse("2019-05-08 19:18:21"));
 		assert json != null;
 		Assert.assertEquals("{\"date\":1557314301000}", json.toString());
 
-		json = Objects.requireNonNull(JSONUtil.createObj().put("date", DateUtil.parse("2019-05-08 19:18:21"))).setDateFormat(DatePattern.NORM_DATE_PATTERN);
+		json = Objects.requireNonNull(JSONUtil.createObj().set("date", DateUtil.parse("2019-05-08 19:18:21"))).setDateFormat(DatePattern.NORM_DATE_PATTERN);
 		Assert.assertEquals("{\"date\":\"2019-05-08\"}", json.toString());
 	}
 
+
 	@Test
 	public void putAllTest() {
-		JSONObject json1 = JSONUtil.createObj();
-		json1.put("a", "value1");
-		json1.put("b", "value2");
-		json1.put("c", "value3");
-		json1.put("d", true);
+		JSONObject json1 = JSONUtil.createObj()
+		.set("a", "value1")
+		.set("b", "value2")
+		.set("c", "value3")
+		.set("d", true);
 
-		JSONObject json2 = JSONUtil.createObj();
-		json2.put("a", "value21");
-		json2.put("b", "value22");
+		JSONObject json2 = JSONUtil.createObj()
+		.set("a", "value21")
+		.set("b", "value22");
 
 		// putAll操作会覆盖相同key的值,因此a,b两个key的值改变,c的值不变
 		json1.putAll(json2);
@@ -134,13 +134,14 @@ public class JSONObjectTest {
 		Console.log(json2);
 	}
 
-	@SuppressWarnings("ConstantConditions")
 	@Test
 	public void toBeanTest() {
-		JSONObject subJson = JSONUtil.createObj().put("value1", "strValue1").put("value2", "234");
-		JSONObject json = JSONUtil.createObj().put("strValue", "strTest").put("intValue", 123)
+		JSONObject subJson = JSONUtil.createObj().set("value1", "strValue1").set("value2", "234");
+		JSONObject json = JSONUtil.createObj().set("strValue", "strTest").set("intValue", 123)
 				// 测试空字符串转对象
-				.put("doubleValue", "").put("beanValue", subJson).put("list", JSONUtil.createArray().put("a").put("b")).put("testEnum", "TYPE_A");
+				.set("doubleValue", "")
+				.set("beanValue", subJson)
+				.set("list", JSONUtil.createArray().set("a").set("b")).set("testEnum", "TYPE_A");
 
 		TestBean bean = json.toBean(TestBean.class);
 		Assert.assertEquals("a", bean.getList().get(0));
@@ -153,14 +154,13 @@ public class JSONObjectTest {
 		Assert.assertEquals(TestEnum.TYPE_A, bean.getTestEnum());
 	}
 
-	@SuppressWarnings("ConstantConditions")
 	@Test
 	public void toBeanNullStrTest() {
 		JSONObject json = JSONUtil.createObj()//
-				.put("strValue", "null")//
-				.put("intValue", 123)//
-				.put("beanValue", "null")//
-				.put("list", JSONUtil.createArray().put("a").put("b"));
+				.set("strValue", "null")//
+				.set("intValue", 123)//
+				.set("beanValue", "null")//
+				.set("list", JSONUtil.createArray().set("a").set("b"));
 
 		TestBean bean = json.toBean(TestBean.class);
 		// 当JSON中为字符串"null"时应被当作字符串处理
@@ -222,15 +222,14 @@ public class JSONObjectTest {
 	/**
 	 * 在JSON转Bean过程中,Bean中字段如果为父类定义的泛型类型,则应正确转换,此方法用于测试这类情况
 	 */
-	@SuppressWarnings("ConstantConditions")
 	@Test
 	public void toBeanTest6() {
 		JSONObject json = JSONUtil.createObj()
-				.put("targetUrl", "http://test.com")
-				.put("success", "true")
-				.put("result", JSONUtil.createObj()
-						.put("token", "tokenTest")
-						.put("userId", "测试用户1"));
+				.set("targetUrl", "http://test.com")
+				.set("success", "true")
+				.set("result", JSONUtil.createObj()
+						.set("token", "tokenTest")
+						.set("userId", "测试用户1"));
 
 		TokenAuthWarp2 bean = json.toBean(TokenAuthWarp2.class);
 		Assert.assertEquals("http://test.com", bean.getTargetUrl());
@@ -260,7 +259,8 @@ public class JSONObjectTest {
 		userA.setDate(new Date());
 		userA.setSqs(CollectionUtil.newArrayList(new Seq(null), new Seq("seq2")));
 
-		JSONObject json = JSONUtil.parseObj(userA, false);
+		JSONObject json = JSONUtil.parseObj(userA, false, true);
+
 		Assert.assertTrue(json.containsKey("a"));
 		Assert.assertTrue(json.getJSONArray("sqs").getJSONObject(0).containsKey("seq"));
 	}
@@ -282,10 +282,11 @@ public class JSONObjectTest {
 		Assert.assertEquals(bean.toString(), bean2.toString());
 	}
 
-	@SuppressWarnings("ConstantConditions")
 	@Test
 	public void parseBeanTest3() {
-		JSONObject json = JSONUtil.createObj().put("code", 22).put("data", "{\"jobId\": \"abc\", \"videoUrl\": \"http://a.com/a.mp4\"}");
+		JSONObject json = JSONUtil.createObj()
+				.set("code", 22)
+				.set("data", "{\"jobId\": \"abc\", \"videoUrl\": \"http://a.com/a.mp4\"}");
 
 		JSONBean bean = json.toBean(JSONBean.class);
 		Assert.assertEquals(22, bean.getCode());
@@ -322,10 +323,12 @@ public class JSONObjectTest {
 		Assert.assertEquals(DateUtil.parse("2018-10-25"), bean.getDate());
 	}
 
-	@SuppressWarnings("ConstantConditions")
 	@Test
 	public void beanTransTest3() {
-		JSONObject userAJson = JSONUtil.createObj().put("a", "AValue").put("name", "nameValue").put("date", "08:00:00");
+		JSONObject userAJson = JSONUtil.createObj()
+				.set("a", "AValue")
+				.set("name", "nameValue")
+				.set("date", "08:00:00");
 		UserA bean = JSONUtil.toBean(userAJson.toString(), UserA.class);
 		Assert.assertEquals(DateUtil.today() + " 08:00:00", DateUtil.date(bean.getDate()).toString());
 	}
@@ -377,10 +380,9 @@ public class JSONObjectTest {
 		Assert.assertEquals("张三", jsonObject.getStr("name"));
 		Assert.assertEquals(new Integer(35), jsonObject.getInt("age"));
 
-		@SuppressWarnings("ConstantConditions")
 		JSONObject json = JSONUtil.createObj()
-				.put("name", "张三")
-				.put("age", 35);
+				.set("name", "张三")
+				.set("age", 35);
 		final BeanWithAlias bean = JSONUtil.toBean(Objects.requireNonNull(json).toString(), BeanWithAlias.class);
 		Assert.assertEquals("张三", bean.getValue1());
 		Assert.assertEquals(new Integer(35), bean.getValue2());

+ 3 - 29
hutool-json/src/test/java/cn/hutool/json/test/bean/UserA.java

@@ -1,40 +1,14 @@
 package cn.hutool.json.test.bean;
 
+import lombok.Data;
+
 import java.util.Date;
 import java.util.List;
 
+@Data
 public class UserA {
 	private String name;
 	private String a;
 	private Date date;
 	private List<Seq> sqs;
-	
-	public String getName() {
-		return name;
-	}
-	public void setName(String name) {
-		this.name = name;
-	}
-	public String getA() {
-		return a;
-	}
-	public void setA(String a) {
-		this.a = a;
-	}
-	public Date getDate() {
-		return date;
-	}
-	public void setDate(Date date) {
-		this.date = date;
-	}
-	public List<Seq> getSqs() {
-		return sqs;
-	}
-	public void setSqs(List<Seq> sqs) {
-		this.sqs = sqs;
-	}
-	@Override
-	public String toString() {
-		return "UserA [name=" + name + ", a=" + a + ", date=" + date + ", sqs=" + sqs + "]";
-	}
 }