Skip to content

Commit 34e537d

Browse files
author
Charles Greer
committed
Issue #249 fixes replaceValue with numbers or booleans
1 parent 3de6bf8 commit 34e537d

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

src/main/java/com/marklogic/client/impl/DocumentPatchBuilderImpl.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,23 +86,32 @@ static class ContentReplaceOperation extends PatchOperation {
8686
String selectPath;
8787
Cardinality cardinality;
8888
boolean isFragment = true;
89-
String input;
89+
Object input;
90+
String inputAsString;
9091
ContentReplaceOperation(String selectPath, Cardinality cardinality, boolean isFragment,
9192
Object input
9293
) {
9394
super();
9495
this.selectPath = selectPath;
9596
this.cardinality = cardinality;
9697
this.isFragment = isFragment;
97-
this.input = (input instanceof String) ?
98-
(String) input : input.toString();
98+
this.input = input;
99+
this.inputAsString = null;
100+
if (input != null) {
101+
inputAsString = (input instanceof String) ? (String) input : input.toString();
102+
}
99103
}
100104
@Override
101105
public void write(JSONStringWriter serializer) {
102106
writeStartReplace(serializer, selectPath, cardinality);
103107
serializer.writeStartEntry("content");
104108
if (isFragment) {
105-
serializer.writeFragment(input);
109+
serializer.writeFragment(inputAsString);
110+
} else if (input instanceof Boolean) {
111+
serializer.writeBooleanValue(input);
112+
}
113+
else if (input instanceof Number) {
114+
serializer.writeNumberValue(input);
106115
} else {
107116
serializer.writeStringValue(input);
108117
}
@@ -115,9 +124,9 @@ public void write(XMLOutputSerializer out) throws Exception {
115124
writeStartReplace(out, selectPath, cardinality);
116125
if (isFragment) {
117126
serializer.writeCharacters(""); // force the tag close
118-
out.getWriter().write(input);
127+
out.getWriter().write(inputAsString);
119128
} else {
120-
serializer.writeCharacters(input);
129+
serializer.writeCharacters(inputAsString);
121130
}
122131
serializer.writeEndElement();
123132
}

src/main/java/com/marklogic/client/impl/JSONStringWriter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,5 @@ and the control characters (U+0000 through U+001F)
153153

154154
return out.toString();
155155
}
156+
156157
}

src/test/java/com/marklogic/client/test/JSONDocumentTest.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.fasterxml.jackson.databind.JsonNode;
3636
import com.fasterxml.jackson.databind.ObjectMapper;
3737
import com.fasterxml.jackson.databind.node.ArrayNode;
38+
import com.fasterxml.jackson.databind.node.BooleanNode;
3839
import com.fasterxml.jackson.databind.node.ObjectNode;
3940
import com.marklogic.client.document.DocumentManager.Metadata;
4041
import com.marklogic.client.document.DocumentMetadataPatchBuilder.Cardinality;
@@ -153,6 +154,9 @@ public void testJsonPathPatch() throws IOException {
153154
fragment = mapper.writeValueAsString(fragmentNode);
154155
patchBldr.insertFragment("$.[\"arrayKey\"]", Position.LAST_CHILD,
155156
Cardinality.ZERO_OR_ONE, fragment);
157+
patchBldr.replaceValue("$.booleanKey", true);
158+
patchBldr.replaceValue("$.numberKey2", 2);
159+
patchBldr.replaceValue("$.nullKey", null);
156160

157161
DocumentPatchHandle patchHandle = patchBldr.pathLanguage(
158162
PathLanguage.JSONPATH).build();
@@ -179,6 +183,10 @@ public void testJsonPathPatch() throws IOException {
179183
childNode.put("appendedKey", "appended item");
180184
childArray.add(childNode);
181185
expectedNode.set("arrayKey", childArray);
186+
expectedNode.put("booleanKey", true);
187+
expectedNode.put("numberKey2", 2);
188+
expectedNode.putNull("nullKey");
189+
182190

183191
String docText = docMgr.read(docId, new StringHandle()).get();
184192
assertNotNull("Read null string for patched JSON content", docText);
@@ -272,6 +280,9 @@ public void testXPathPatch() throws IOException {
272280
fragment = mapper.writeValueAsString(fragmentNode);
273281
patchBldr.insertFragment("/array-node('arrayKey')", Position.BEFORE,
274282
fragment);
283+
patchBldr.replaceValue("/booleanKey", true);
284+
patchBldr.replaceValue("/numberKey2", 2);
285+
patchBldr.replaceValue("/nullKey", null);
275286

276287
//patchBldr.replaceApply("/node()/arrayKey/node()[string(.) eq '3']",
277288
// patchBldr.call().add(2));
@@ -306,18 +317,19 @@ public void testXPathPatch() throws IOException {
306317
childNode.put("appendedKey", "appended item");
307318
childArray.add(childNode);
308319
expectedNode.set("arrayKey", childArray);
320+
expectedNode.put("booleanKey", true);
321+
expectedNode.put("numberKey2", 2);
322+
expectedNode.putNull("nullKey");
309323

310324
String docText = docMgr.read(docId, new StringHandle()).get();
311-
325+
312326
assertNotNull("Read null string for patched JSON content", docText);
313327
JsonNode readNode = mapper.readTree(docText);
314-
315328

316329
logger.debug("Before3:" + content);
317330
logger.debug("After3:"+docText);
318331
logger.debug("Expected3:" + mapper.writeValueAsString(expectedNode));
319-
320-
332+
321333
assertTrue("Patched JSON document without expected result",
322334
expectedNode.equals(readNode));
323335

@@ -400,6 +412,9 @@ private ObjectNode makeContent(ObjectMapper mapper) throws IOException {
400412
childNode.put("itemObjectKey", "item object value");
401413
childArray.add(childNode);
402414
sourceNode.set("arrayKey", childArray);
415+
sourceNode.put("booleanKey", false);
416+
sourceNode.put("numberKey2", 1);
417+
sourceNode.put("nullKey", 0);
403418

404419
return sourceNode;
405420
}

0 commit comments

Comments
 (0)