Skip to content

Commit a6a670c

Browse files
committed
new methods for update and notify
1 parent 6134019 commit a6a670c

File tree

3 files changed

+65
-60
lines changed

3 files changed

+65
-60
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<groupId>com.github.leonardofel</groupId>
55
<artifactId>json-java-put-null-fix</artifactId>
6-
<version>3.0.43.unsafe</version>
6+
<version>3.0.44.unsafe</version>
77
<packaging>jar</packaging>
88

99
<name>JSON in Java WITH WORKING .put(null)</name>

src/main/java/org/json/JSONObject.java

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,7 +1793,8 @@ public JSONObject putMap(String key, Map<?, ?> value) throws JSONException {
17931793
return this.put(key, new JSONObject(value));
17941794
}
17951795

1796-
private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
1796+
private PropertyChangeSupport propertyChangeSupportUpdate = new PropertyChangeSupport(this);
1797+
private PropertyChangeSupport propertyChangeSupportNotify = new PropertyChangeSupport(this);
17971798
private static final String propertyChangeGlobalKeyword = "__THIS__";
17981799
/**
17991800
* Add a PropertyChangeListener to the JSONObject.
@@ -1804,8 +1805,8 @@ public JSONObject putMap(String key, Map<?, ?> value) throws JSONException {
18041805
*
18051806
* @param listener The PropertyChangeListener to be added
18061807
*/
1807-
public void addUpdateListenerGlobal(PropertyChangeListener listener) {
1808-
this.propertyChangeSupport.addPropertyChangeListener(JSONObject.propertyChangeGlobalKeyword, listener);
1808+
public void onUpdateGlobal(PropertyChangeListener listener) {
1809+
this.propertyChangeSupportUpdate.addPropertyChangeListener(JSONObject.propertyChangeGlobalKeyword, listener);
18091810
}
18101811

18111812
/**
@@ -1820,14 +1821,18 @@ public void addUpdateListenerGlobal(PropertyChangeListener listener) {
18201821
* @param key The key string to listen on.
18211822
* @param listener The PropertyChangeListener to be added
18221823
*/
1823-
public void addUpdateListener(String key, PropertyChangeListener listener) {
1824+
public void onUpdate(String key, PropertyChangeListener listener) {
18241825
if (JSONObject.propertyChangeGlobalKeyword.equals(key)) {
18251826
throw new JSONException("key \"" + JSONObject.propertyChangeGlobalKeyword + "\" is reserved");
18261827
} else {
1827-
this.propertyChangeSupport.addPropertyChangeListener(key, listener);
1828+
this.propertyChangeSupportUpdate.addPropertyChangeListener(key, listener);
18281829
}
18291830
}
18301831

1832+
public void onNotify(String key, PropertyChangeListener listener) {
1833+
this.propertyChangeSupportNotify.addPropertyChangeListener(key, listener);
1834+
}
1835+
18311836
/**
18321837
* Put a key/value pair in the JSONObject and
18331838
* reports a bound property update to listeners.
@@ -1849,11 +1854,16 @@ public JSONObject update(String key, Object newValue) throws JSONException {
18491854
final Object oldValue = this.opt(key);
18501855
this.put(key, newValue);
18511856

1852-
this.propertyChangeSupport.firePropertyChange(JSONObject.propertyChangeGlobalKeyword, oldThis, this);
1857+
this.propertyChangeSupportUpdate.firePropertyChange(JSONObject.propertyChangeGlobalKeyword, oldThis, this);
1858+
this.propertyChangeSupportUpdate.firePropertyChange(key, oldValue, newValue);
18531859

1854-
if (this.propertyChangeSupport.hasListeners(key)) {
1855-
this.propertyChangeSupport.firePropertyChange(key, oldValue, newValue);
1856-
}
1860+
this.propertyChangeSupportNotify.firePropertyChange(key, oldValue, newValue);
1861+
1862+
return this;
1863+
}
1864+
1865+
public JSONObject notify(String key, Object oldValue, Object newValue) throws JSONException {
1866+
this.propertyChangeSupportNotify.firePropertyChange(key, oldValue, newValue);
18571867

18581868
return this;
18591869
}
@@ -1906,7 +1916,7 @@ public JSONObject mixOrRemove(JSONObject jo) throws JSONException {
19061916
return this.updateOrRemove(jo, true, false);
19071917
}
19081918

1909-
private JSONObject updateOrRemove(JSONObject jo, boolean remove, boolean triggerEvent) throws JSONException {
1919+
private JSONObject updateOrRemove(JSONObject jo, boolean remove, boolean triggerUpdate) throws JSONException {
19101920
final JSONObject oldThis = new JSONObject(this.toString());
19111921

19121922
final HashMap<String, Object> oldValues = new HashMap<String, Object>();
@@ -1937,24 +1947,19 @@ private JSONObject updateOrRemove(JSONObject jo, boolean remove, boolean trigger
19371947
}
19381948
}
19391949

1940-
if (triggerEvent) {
1941-
this.propertyChangeSupport.firePropertyChange(JSONObject.propertyChangeGlobalKeyword, oldThis, this);
1950+
if (oldValues.size() > 0) {
1951+
if (triggerUpdate) {
1952+
this.propertyChangeSupportUpdate.firePropertyChange(JSONObject.propertyChangeGlobalKeyword, oldThis, this);
1953+
}
19421954

19431955
oldValues.forEach((key, oldValue) -> {
1944-
if (this.propertyChangeSupport.hasListeners(key)) {
1945-
final Object newValue;
1946-
if (remove && delValues.contains(key)) {
1947-
newValue = null;
1948-
} else {
1949-
newValue = newValues.get(key);
1950-
}
1956+
final Object v2 = delValues.contains(key) ? null : newValues.get(key);
1957+
final Object v1 = oldValue == null && v2 == null ? JSONObject.NULL : oldValue;
19511958

1952-
if (oldValue == null && newValue == null) {
1953-
this.propertyChangeSupport.firePropertyChange(key, JSONObject.NULL, newValue);
1954-
} else {
1955-
this.propertyChangeSupport.firePropertyChange(key, oldValue, newValue);
1956-
}
1959+
if (triggerUpdate) {
1960+
this.propertyChangeSupportUpdate.firePropertyChange(key, v1, v2);
19571961
}
1962+
this.propertyChangeSupportNotify.firePropertyChange(key, v1, v2);
19581963
});
19591964
}
19601965

src/test/java/org/json/junit/JSONTest.java

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -425,17 +425,17 @@ public void updateNotEqualsTest() {
425425

426426
//assertThrows(JSONException.class, () -> j.update("myMapListener", "propertyChange"));
427427

428-
j.addUpdateListenerGlobal(evt -> {
428+
j.onUpdateGlobal(evt -> {
429429
final Object oldValue = evt.getOldValue();
430430
assertEquals("{}", oldValue.toString());
431431
});
432432

433-
j.addUpdateListenerGlobal(evt -> {
433+
j.onUpdateGlobal(evt -> {
434434
final Object newValue = evt.getNewValue();
435435
assertEquals("{\"myMapListener\":\"propertyChange\"}", newValue.toString());
436436
});
437437

438-
j.addUpdateListener("myMapListener", evt -> {
438+
j.onUpdate("myMapListener", evt -> {
439439
final Object oldValue = evt.getOldValue();
440440
final Object newValue = evt.getNewValue();
441441

@@ -453,19 +453,19 @@ public void updateListenerGlobalTest() {
453453
final AtomicInteger globalExecutions = new AtomicInteger();
454454
assertEquals(counter.get(), globalExecutions.get());
455455

456-
j.addUpdateListenerGlobal(evt -> {
456+
j.onUpdateGlobal(evt -> {
457457
globalExecutions.incrementAndGet();
458458
});
459459

460-
j.addUpdateListenerGlobal(evt -> {
460+
j.onUpdateGlobal(evt -> {
461461
assertEquals(counter.incrementAndGet(), globalExecutions.get() * 3 - 2);
462462
});
463463

464-
j.addUpdateListenerGlobal(evt -> {
464+
j.onUpdateGlobal(evt -> {
465465
assertEquals(counter.incrementAndGet(), globalExecutions.get() * 3 - 1);
466466
});
467467

468-
j.addUpdateListener("myMapListener", evt -> {
468+
j.onUpdate("myMapListener", evt -> {
469469
assertEquals(counter.incrementAndGet(), globalExecutions.get() * 3 - 0);
470470
});
471471

@@ -482,13 +482,13 @@ public void updateListenerTest() {
482482

483483
j.put("myMapListener", "unchangedProperty");
484484

485-
j.addUpdateListener("myMapListener", evt -> {
485+
j.onUpdate("myMapListener", evt -> {
486486
fail("They are the same");
487487
});
488488

489489
j.update("myMapListener", "unchangedProperty");
490490

491-
j.addUpdateListener("otherMapListener", evt -> {
491+
j.onUpdate("otherMapListener", evt -> {
492492
final Object oldValue = evt.getOldValue();
493493
assertEquals(oldValue, null);
494494

@@ -525,42 +525,42 @@ public void updateListener2Test() {
525525

526526
final JSONObject oldJsonObject2 = new JSONObject(jsonObject2.toString());
527527

528-
jsonObject1.addUpdateListenerGlobal(evt -> {
528+
jsonObject1.onUpdateGlobal(evt -> {
529529
final Object oldValue = evt.getOldValue();
530530
final Object newValue = evt.getNewValue();
531531

532532
assertNotEquals(oldValue.toString(), newValue.toString());
533533
});
534534

535-
jsonObject1.addUpdateListener("trueKey", evt -> {
535+
jsonObject1.onUpdate("trueKey", evt -> {
536536
assertEquals(Boolean.valueOf(true), evt.getOldValue());
537537
assertNull(evt.getNewValue());
538538
});
539-
jsonObject1.addUpdateListener("falseKey", evt -> {
539+
jsonObject1.onUpdate("falseKey", evt -> {
540540
assertEquals(Boolean.valueOf(false), evt.getOldValue());
541541
assertNull(evt.getNewValue());
542542
});
543-
jsonObject1.addUpdateListener("stringKey", evt -> {
543+
jsonObject1.onUpdate("stringKey", evt -> {
544544
assertEquals("CHANGE ME!!!", evt.getOldValue());
545545
assertEquals("hello world!", evt.getNewValue());
546546
});
547-
jsonObject1.addUpdateListener("nullKey", evt -> {
547+
jsonObject1.onUpdate("nullKey", evt -> {
548548
assertNull(evt.getOldValue());
549549
assertNull(null, evt.getNewValue());
550550
});
551-
jsonObject1.addUpdateListener("nullBefore", evt -> {
551+
jsonObject1.onUpdate("nullBefore", evt -> {
552552
assertNull(evt.getOldValue());
553553
assertEquals("null", evt.getNewValue());
554554
});
555-
jsonObject1.addUpdateListener("nullAfter", evt -> {
555+
jsonObject1.onUpdate("nullAfter", evt -> {
556556
assertEquals("null", evt.getOldValue());
557557
assertEquals(null, evt.getNewValue());
558558
});
559-
jsonObject1.addUpdateListener("escapeStringKey", evt -> {
559+
jsonObject1.onUpdate("escapeStringKey", evt -> {
560560
assertNull(evt.getOldValue());
561561
assertEquals("h\be\tllo w\u1234orld!", evt.getNewValue());
562562
});
563-
jsonObject1.addUpdateListener("intKey", evt -> {
563+
jsonObject1.onUpdate("intKey", evt -> {
564564
assertNull(evt.getOldValue());
565565
assertEquals(42, evt.getNewValue());
566566
});
@@ -572,35 +572,35 @@ public void updateListener2Test() {
572572
assertNotEquals(jsonObject1.toString(), oldJsonObject1.toString());
573573
assertEquals(jsonObject2.toString(), oldJsonObject2.toString());
574574

575-
oldJsonObject1.addUpdateListener("trueKey", evt -> {
575+
oldJsonObject1.onUpdate("trueKey", evt -> {
576576
assertEquals(Boolean.valueOf(true), evt.getOldValue());
577577
assertNull(evt.getNewValue());
578578
});
579-
oldJsonObject1.addUpdateListener("falseKey", evt -> {
579+
oldJsonObject1.onUpdate("falseKey", evt -> {
580580
assertEquals(Boolean.valueOf(false), evt.getOldValue());
581581
assertNull(evt.getNewValue());
582582
});
583-
oldJsonObject1.addUpdateListener("stringKey", evt -> {
583+
oldJsonObject1.onUpdate("stringKey", evt -> {
584584
assertEquals("CHANGE ME!!!", evt.getOldValue());
585585
assertEquals("hello world!", evt.getNewValue());
586586
});
587-
oldJsonObject1.addUpdateListener("nullKey", evt -> {
587+
oldJsonObject1.onUpdate("nullKey", evt -> {
588588
assertNull(evt.getOldValue());
589589
assertNull(null, evt.getNewValue());
590590
});
591-
oldJsonObject1.addUpdateListener("nullBefore", evt -> {
591+
oldJsonObject1.onUpdate("nullBefore", evt -> {
592592
assertNull(evt.getOldValue());
593593
assertEquals("null", evt.getNewValue());
594594
});
595-
oldJsonObject1.addUpdateListener("nullAfter", evt -> {
595+
oldJsonObject1.onUpdate("nullAfter", evt -> {
596596
assertEquals("null", evt.getOldValue());
597597
assertEquals(null, evt.getNewValue());
598598
});
599-
oldJsonObject1.addUpdateListener("escapeStringKey", evt -> {
599+
oldJsonObject1.onUpdate("escapeStringKey", evt -> {
600600
assertNull(evt.getOldValue());
601601
assertEquals("h\be\tllo w\u1234orld!", evt.getNewValue());
602602
});
603-
oldJsonObject1.addUpdateListener("intKey", evt -> {
603+
oldJsonObject1.onUpdate("intKey", evt -> {
604604
assertNull(evt.getOldValue());
605605
assertEquals(42, evt.getNewValue());
606606
});
@@ -623,7 +623,7 @@ public void updateOrRemoveSrcEmptyTest() {
623623
.put("stringKey", "hello world!")
624624
.put("nullKey", null);
625625

626-
jsonObject1.addUpdateListenerGlobal(evt -> {
626+
jsonObject1.onUpdateGlobal(evt -> {
627627
final Object oldValue = evt.getOldValue();
628628
final Object newValue = evt.getNewValue();
629629

@@ -647,7 +647,7 @@ public void updateOrRemoveDstEmptyTest() {
647647
.put("nullKey", null);
648648
final JSONObject jsonObject2 = new JSONObject();
649649

650-
jsonObject1.addUpdateListenerGlobal(evt -> {
650+
jsonObject1.onUpdateGlobal(evt -> {
651651
final Object oldValue = evt.getOldValue();
652652
final Object newValue = evt.getNewValue();
653653

@@ -667,7 +667,7 @@ public void updateOrRemoveAllEmptyTest() {
667667
final JSONObject jsonObject1 = new JSONObject();
668668
final JSONObject jsonObject2 = new JSONObject();
669669

670-
jsonObject1.addUpdateListenerGlobal(evt -> {
670+
jsonObject1.onUpdateGlobal(evt -> {
671671
final Object oldValue = evt.getOldValue();
672672
final Object newValue = evt.getNewValue();
673673

@@ -688,15 +688,15 @@ public void updateOrRemoveSrcTest() {
688688
.put("stringKey", "hello world!");
689689
final JSONObject jsonObject2 = new JSONObject();
690690

691-
jsonObject1.addUpdateListenerGlobal(evt -> {
691+
jsonObject1.onUpdateGlobal(evt -> {
692692
final Object oldValue = evt.getOldValue();
693693
final Object newValue = evt.getNewValue();
694694

695695
assertEquals("{\"stringKey\":\"hello world!\"}", oldValue.toString());
696696
assertEquals("{}", newValue.toString());
697697
});
698698

699-
jsonObject1.addUpdateListener("stringKey", evt -> {
699+
jsonObject1.onUpdate("stringKey", evt -> {
700700
final Object oldValue = evt.getOldValue();
701701
final Object newValue = evt.getNewValue();
702702

@@ -718,15 +718,15 @@ public void updateOrRemoveDstTest() {
718718
final JSONObject jsonObject2 = new JSONObject()
719719
.put("stringKey", "hello world!");
720720

721-
jsonObject1.addUpdateListenerGlobal(evt -> {
721+
jsonObject1.onUpdateGlobal(evt -> {
722722
final Object oldValue = evt.getOldValue();
723723
final Object newValue = evt.getNewValue();
724724

725725
assertEquals("{}", oldValue.toString());
726726
assertEquals("{\"stringKey\":\"hello world!\"}", newValue.toString());
727727
});
728728

729-
jsonObject1.addUpdateListener("stringKey", evt -> {
729+
jsonObject1.onUpdate("stringKey", evt -> {
730730
final Object oldValue = evt.getOldValue();
731731
final Object newValue = evt.getNewValue();
732732

@@ -748,15 +748,15 @@ public void newNullTest() {
748748
final JSONObject jsonObject2 = new JSONObject()
749749
.put("stringKey", "hello world!");
750750

751-
jsonObject1.addUpdateListenerGlobal(evt -> {
751+
jsonObject1.onUpdateGlobal(evt -> {
752752
final Object oldValue = evt.getOldValue();
753753
final Object newValue = evt.getNewValue();
754754

755755
assertEquals("{}", oldValue.toString());
756756
assertEquals("{\"stringKey\":\"hello world!\"}", newValue.toString());
757757
});
758758

759-
jsonObject1.addUpdateListener("stringKey", evt -> {
759+
jsonObject1.onUpdate("stringKey", evt -> {
760760
final Object oldValue = evt.getOldValue();
761761
final Object newValue = evt.getNewValue();
762762

0 commit comments

Comments
 (0)