share PropertyUtils if not explisitly set in Constructor or Representer
diff --git a/src/main/java/org/yaml/snakeyaml/Yaml.java b/src/main/java/org/yaml/snakeyaml/Yaml.java
index 338521f..347e45a 100644
--- a/src/main/java/org/yaml/snakeyaml/Yaml.java
+++ b/src/main/java/org/yaml/snakeyaml/Yaml.java
@@ -149,6 +149,11 @@
*/
public Yaml(BaseConstructor constructor, Representer representer, DumperOptions options,
Resolver resolver) {
+ if(!constructor.isExplicitPropertyUtils()) {
+ constructor.setPropertyUtils(representer.getPropertyUtils());
+ } else if(!representer.isExplicitPropertyUtils()) {
+ representer.setPropertyUtils(constructor.getPropertyUtils());
+ }
this.constructor = constructor;
representer.setDefaultFlowStyle(options.getDefaultFlowStyle());
representer.setDefaultScalarStyle(options.getDefaultScalarStyle());
@@ -478,6 +483,7 @@
public void setBeanAccess(BeanAccess beanAccess) {
constructor.getPropertyUtils().setBeanAccess(beanAccess);
+ representer.getPropertyUtils().setBeanAccess(beanAccess);
}
// deprecated
diff --git a/src/main/java/org/yaml/snakeyaml/constructor/BaseConstructor.java b/src/main/java/org/yaml/snakeyaml/constructor/BaseConstructor.java
index 6e01224..1d582c3 100644
--- a/src/main/java/org/yaml/snakeyaml/constructor/BaseConstructor.java
+++ b/src/main/java/org/yaml/snakeyaml/constructor/BaseConstructor.java
@@ -72,6 +72,7 @@
protected Tag rootTag;
private PropertyUtils propertyUtils;
+ private boolean explicitPropertyUtils;
public BaseConstructor() {
constructedObjects = new HashMap<Node, Object>();
@@ -79,6 +80,7 @@
maps2fill = new ArrayList<RecursiveTuple<Map<Object, Object>, RecursiveTuple<Object, Object>>>();
sets2fill = new ArrayList<RecursiveTuple<Set<Object>, Object>>();
rootTag = null;
+ explicitPropertyUtils = false;
}
public void setComposer(Composer composer) {
@@ -380,6 +382,7 @@
public void setPropertyUtils(PropertyUtils propertyUtils) {
this.propertyUtils = propertyUtils;
+ explicitPropertyUtils = true;
}
public final PropertyUtils getPropertyUtils() {
@@ -406,4 +409,8 @@
return _1;
}
}
+
+ public final boolean isExplicitPropertyUtils() {
+ return explicitPropertyUtils ;
+ }
}
diff --git a/src/main/java/org/yaml/snakeyaml/representer/BaseRepresenter.java b/src/main/java/org/yaml/snakeyaml/representer/BaseRepresenter.java
index c0b5285..b066b88 100644
--- a/src/main/java/org/yaml/snakeyaml/representer/BaseRepresenter.java
+++ b/src/main/java/org/yaml/snakeyaml/representer/BaseRepresenter.java
@@ -61,6 +61,7 @@
private final Set<Object> objectKeeper = new HashSet<Object>();
protected Object objectToRepresent;
private PropertyUtils propertyUtils;
+ private boolean explicitPropertyUtils = false;
public void represent(Serializer serializer, Object data) throws IOException {
Node node = representData(data);
@@ -195,6 +196,7 @@
public void setPropertyUtils(PropertyUtils propertyUtils) {
this.propertyUtils = propertyUtils;
+ this.explicitPropertyUtils = true;
}
public final PropertyUtils getPropertyUtils() {
@@ -204,4 +206,8 @@
return propertyUtils;
}
+ public final boolean isExplicitPropertyUtils() {
+ return explicitPropertyUtils;
+ }
+
}
diff --git a/src/test/java/org/yaml/snakeyaml/PropertyUtilsSharingTest.java b/src/test/java/org/yaml/snakeyaml/PropertyUtilsSharingTest.java
new file mode 100644
index 0000000..aaa4f9f
--- /dev/null
+++ b/src/test/java/org/yaml/snakeyaml/PropertyUtilsSharingTest.java
@@ -0,0 +1,73 @@
+/**
+ * Copyright (c) 2008-2010, http://code.google.com/p/snakeyaml/
+ *
+ * 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.yaml.snakeyaml;
+
+import static org.junit.Assert.assertSame;
+
+import org.junit.Test;
+import org.yaml.snakeyaml.constructor.Constructor;
+import org.yaml.snakeyaml.introspector.PropertyUtils;
+import org.yaml.snakeyaml.representer.Representer;
+
+public class PropertyUtilsSharingTest {
+
+ @Test
+ public void testYamlDefaults() {
+ Yaml yaml1 = new Yaml();
+ assertSame(yaml1.constructor.getPropertyUtils(), yaml1.representer.getPropertyUtils());
+
+ Yaml yaml2 = new Yaml(new Constructor());
+ assertSame(yaml2.constructor.getPropertyUtils(), yaml2.representer.getPropertyUtils());
+
+ Yaml yaml3 = new Yaml(new Representer());
+ assertSame(yaml3.constructor.getPropertyUtils(), yaml3.representer.getPropertyUtils());
+ }
+
+ @Test
+ public void testYamlConstructorWithPropertyUtils() {
+ Constructor constructor1 = new Constructor();
+ PropertyUtils pu = new PropertyUtils();
+ constructor1.setPropertyUtils(pu);
+ Yaml yaml = new Yaml(constructor1);
+ assertSame(pu, yaml.constructor.getPropertyUtils());
+ assertSame(pu, yaml.representer.getPropertyUtils());
+ }
+
+ @Test
+ public void testYamlRepresenterWithPropertyUtils() {
+ Representer representer2 = new Representer();
+ PropertyUtils pu = new PropertyUtils();
+ representer2.setPropertyUtils(pu);
+ Yaml yaml = new Yaml(representer2);
+ assertSame(pu, yaml.constructor.getPropertyUtils());
+ assertSame(pu, yaml.representer.getPropertyUtils());
+ }
+
+ @Test
+ public void testYamlConstructorANDRepresenterWithPropertyUtils() {
+ Constructor constructor = new Constructor();
+ PropertyUtils pu_c = new PropertyUtils();
+ constructor.setPropertyUtils(pu_c);
+ Representer representer = new Representer();
+ PropertyUtils pu_r = new PropertyUtils();
+ representer.setPropertyUtils(pu_r);
+ Yaml yaml = new Yaml(constructor, representer);
+ assertSame(pu_c, yaml.constructor.getPropertyUtils());
+ assertSame(pu_r, yaml.representer.getPropertyUtils());
+ }
+
+}