Skip to content

Commit 83e3cac

Browse files
author
graeme
committed
additional files from GRAILS-1702
git-svn-id: https://svn.codehaus.org/grails/trunk@5784 1cfb16fd-6d17-0410-8ff1-b7e8e1e2867d
1 parent c69c2d0 commit 83e3cac

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/* Copyright 2006-2007 Graeme Rocher
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
package grails.converters.deep;
16+
17+
import org.codehaus.groovy.grails.web.converters.Converter;
18+
import org.codehaus.groovy.grails.web.converters.exceptions.ConverterException;
19+
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
import java.util.Stack;
23+
24+
/**
25+
* A converter that converts domain classes, Maps, Lists, Arrays, POJOs and POGOs
26+
* to JSON (Including nested Domain Classes)
27+
*
28+
* @author Siegfried Puchbauer
29+
*/
30+
public class JSON extends grails.converters.JSON implements Converter {
31+
32+
private Stack stack = new Stack();
33+
34+
protected void bean(Object o) throws ConverterException {
35+
if (stack.contains(o)) {
36+
//value(new HashMap());//throw new ConverterException("Circular Relationship detected!");
37+
handleCircularRelationship(o);
38+
return;
39+
}
40+
stack.push(o);
41+
super.bean(o);
42+
stack.pop();
43+
}
44+
45+
protected void domain(Object o) throws ConverterException {
46+
if (stack.contains(o)) {
47+
//value(new HashMap());//throw new ConverterException("Circular Relationship detected!");
48+
handleCircularRelationship(o);
49+
return;
50+
}
51+
stack.push(o);
52+
super.domain(o);
53+
stack.pop();
54+
}
55+
56+
protected void handleCircularRelationship(Object o) throws ConverterException {
57+
Map props = new HashMap();
58+
props.put("class", o.getClass());
59+
StringBuffer ref = new StringBuffer();
60+
int idx = stack.indexOf(o);
61+
for (int i = stack.size(); i > idx; i--) {
62+
ref.append("../");
63+
}
64+
props.put("_ref", ref.substring(0, ref.length() - 1));
65+
value(props);
66+
}
67+
68+
/**
69+
* internal Getter
70+
*
71+
* @return true
72+
*/
73+
public boolean isRenderDomainClassRelations() {
74+
return true;
75+
}
76+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* Copyright 2006-2007 Graeme Rocher
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
package grails.converters.deep;
16+
17+
import com.thoughtworks.xstream.XStream;
18+
import org.codehaus.groovy.grails.web.converters.Converter;
19+
import org.codehaus.groovy.grails.web.converters.xtream.DomainClassConverter;
20+
21+
/**
22+
* A converter that converts domain classes to XML (including nested Domain Classes)
23+
*
24+
* @author Siegfried Puchbauer
25+
*/
26+
public class XML extends grails.converters.XML implements Converter {
27+
28+
/**
29+
* Configures the XStream instance
30+
*
31+
* @param xs an XStream instance
32+
*/
33+
public void configureXStream(XStream xs) {
34+
DomainClassConverter dcConverter = new DomainClassConverter();
35+
dcConverter.setRenderDomainClassRelations(true);
36+
xs.registerConverter(dcConverter);
37+
}
38+
39+
}

0 commit comments

Comments
 (0)