You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by ch...@apache.org on 2017/07/21 05:24:13 UTC
svn commit: r1802543 - in /jackrabbit/oak/trunk/oak-store-spi/src:
main/java/org/apache/jackrabbit/oak/json/JsonSerializer.java
test/java/org/apache/jackrabbit/oak/json/JsonSerializerTest.java
Author: chetanm
Date: Fri Jul 21 05:24:13 2017
New Revision: 1802543
URL: http://svn.apache.org/viewvc?rev=1802543&view=rev
Log:
OAK-6476 - Support deserializing json as NodeState
Support emitting child nodes in the order of names as in ":childOrder"
property
Added:
jackrabbit/oak/trunk/oak-store-spi/src/test/java/org/apache/jackrabbit/oak/json/JsonSerializerTest.java (with props)
Modified:
jackrabbit/oak/trunk/oak-store-spi/src/main/java/org/apache/jackrabbit/oak/json/JsonSerializer.java
Modified: jackrabbit/oak/trunk/oak-store-spi/src/main/java/org/apache/jackrabbit/oak/json/JsonSerializer.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-store-spi/src/main/java/org/apache/jackrabbit/oak/json/JsonSerializer.java?rev=1802543&r1=1802542&r2=1802543&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-store-spi/src/main/java/org/apache/jackrabbit/oak/json/JsonSerializer.java (original)
+++ jackrabbit/oak/trunk/oak-store-spi/src/main/java/org/apache/jackrabbit/oak/json/JsonSerializer.java Fri Jul 21 05:24:13 2017
@@ -22,6 +22,7 @@ import static org.apache.jackrabbit.oak.
import static org.apache.jackrabbit.oak.api.Type.BOOLEAN;
import static org.apache.jackrabbit.oak.api.Type.DOUBLE;
import static org.apache.jackrabbit.oak.api.Type.LONG;
+import static org.apache.jackrabbit.oak.api.Type.NAMES;
import static org.apache.jackrabbit.oak.api.Type.STRING;
import java.util.List;
@@ -29,11 +30,14 @@ import java.util.regex.Pattern;
import javax.jcr.PropertyType;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
import org.apache.jackrabbit.oak.commons.json.JsopBuilder;
import org.apache.jackrabbit.oak.api.Blob;
import org.apache.jackrabbit.oak.api.PropertyState;
import org.apache.jackrabbit.oak.api.Type;
import org.apache.jackrabbit.oak.commons.json.JsopTokenizer;
+import org.apache.jackrabbit.oak.plugins.memory.MemoryChildNodeEntry;
import org.apache.jackrabbit.oak.spi.state.ChildNodeEntry;
import org.apache.jackrabbit.oak.spi.state.NodeState;
@@ -104,7 +108,7 @@ public class JsonSerializer {
int index = 0;
int count = 0;
- for (ChildNodeEntry child : node.getChildNodeEntries()) {
+ for (ChildNodeEntry child : getChildNodeEntries(node)) {
String name = child.getName();
if (filter.includeNode(name) && index++ >= offset) {
if (count++ >= maxChildNodes) {
@@ -124,6 +128,19 @@ public class JsonSerializer {
json.endObject();
}
+ private Iterable<? extends ChildNodeEntry> getChildNodeEntries(NodeState node) {
+ PropertyState order = node.getProperty(":childOrder");
+ if (order != null) {
+ List<String> names = ImmutableList.copyOf(order.getValue(NAMES));
+ List<ChildNodeEntry> entries = Lists.newArrayListWithCapacity(names.size());
+ for (String name : names) {
+ entries.add(new MemoryChildNodeEntry(name, node.getChildNode(name)));
+ }
+ return entries;
+ }
+ return node.getChildNodeEntries();
+ }
+
public void serialize(PropertyState property) {
Type<?> type = property.getType();
if (!type.isArray()) {
Added: jackrabbit/oak/trunk/oak-store-spi/src/test/java/org/apache/jackrabbit/oak/json/JsonSerializerTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-store-spi/src/test/java/org/apache/jackrabbit/oak/json/JsonSerializerTest.java?rev=1802543&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-store-spi/src/test/java/org/apache/jackrabbit/oak/json/JsonSerializerTest.java (added)
+++ jackrabbit/oak/trunk/oak-store-spi/src/test/java/org/apache/jackrabbit/oak/json/JsonSerializerTest.java Fri Jul 21 05:24:13 2017
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.jackrabbit.oak.json;
+
+import java.util.Arrays;
+import java.util.List;
+
+import com.google.common.collect.Lists;
+import org.apache.jackrabbit.JcrConstants;
+import org.apache.jackrabbit.oak.api.Tree;
+import org.apache.jackrabbit.oak.api.Type;
+import org.apache.jackrabbit.oak.commons.json.JsopBuilder;
+import org.apache.jackrabbit.oak.commons.json.JsopReader;
+import org.apache.jackrabbit.oak.commons.json.JsopTokenizer;
+import org.apache.jackrabbit.oak.plugins.memory.PropertyStates;
+import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
+import org.apache.jackrabbit.oak.spi.state.NodeState;
+import org.junit.Test;
+
+import static org.apache.jackrabbit.oak.plugins.memory.EmptyNodeState.EMPTY_NODE;
+import static org.junit.Assert.*;
+
+public class JsonSerializerTest {
+
+ @Test
+ public void childOrder() throws Exception{
+ NodeBuilder builder = EMPTY_NODE.builder();
+ builder.child("a");
+ builder.child("b");
+ builder.child("c");
+ List<String> expectedOrder = Arrays.asList("a", "c", "b");
+ builder.setProperty(":childOrder", expectedOrder, Type.NAMES);
+
+ NodeState state = builder.getNodeState();
+ String json = serialize(state);
+
+ JsopReader reader = new JsopTokenizer(json);
+ List<String> childNames = Lists.newArrayList();
+ reader.read('{');
+ do {
+ String key = reader.readString();
+ reader.read(':');
+ if (reader.matches('{')) {
+ childNames.add(key);
+ reader.matches('}');
+ }
+
+ } while (reader.matches(','));
+
+ assertEquals(expectedOrder, childNames);
+ }
+
+ private String serialize(NodeState nodeState){
+ JsopBuilder json = new JsopBuilder();
+ new JsonSerializer(json, "{\"properties\":[\"*\", \"-:*\"]}", new BlobSerializer()).serialize(nodeState);
+ return json.toString();
+ }
+}
\ No newline at end of file
Propchange: jackrabbit/oak/trunk/oak-store-spi/src/test/java/org/apache/jackrabbit/oak/json/JsonSerializerTest.java
------------------------------------------------------------------------------
svn:eol-style = native