jrjackson 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,80 +0,0 @@
1
- package com.jrjackson;
2
-
3
- import org.jruby.Ruby;
4
- import org.jruby.RubyClass;
5
- import org.jruby.RubyObject;
6
- import org.jruby.RubyString;
7
- import org.jruby.RubyIO;
8
- import org.jruby.anno.JRubyMethod;
9
- import org.jruby.anno.JRubyModule;
10
- import org.jruby.exceptions.RaiseException;
11
- import org.jruby.ext.stringio.RubyStringIO;
12
- import org.jruby.java.addons.IOJavaAddons;
13
- import org.jruby.javasupport.JavaUtil;
14
- import org.jruby.runtime.ThreadContext;
15
- import org.jruby.runtime.builtin.IRubyObject;
16
- import org.jruby.util.RubyDateFormat;
17
-
18
- import java.io.InputStream;
19
- import java.io.IOException;
20
- import java.text.DateFormat;
21
- import java.util.*;
22
-
23
- import com.fasterxml.jackson.databind.ObjectMapper;
24
- import com.fasterxml.jackson.databind.module.SimpleModule;
25
- import com.fasterxml.jackson.core.Version;
26
- import com.fasterxml.jackson.core.JsonProcessingException;
27
-
28
- @JRubyModule(name = "JrJacksonSym")
29
- public class JrJacksonSym extends RubyObject {
30
- private static final ObjectMapper mapper = new ObjectMapper();
31
- private static final SimpleModule module = new SimpleModule("JrJacksonSymModule");
32
-
33
- static {
34
- mapper.registerModule(module.addDeserializer(Object.class, RubyObjectSymDeserializer.instance));
35
- mapper.setDateFormat(new RubyDateFormat("yyyy-MM-dd HH:mm:ss z", Locale.US, true));
36
- }
37
-
38
- public JrJacksonSym(Ruby ruby, RubyClass metaclass) {
39
- super(ruby, metaclass);
40
- }
41
-
42
- @JRubyMethod(module = true, name = {"parse", "load"}, required = 1)
43
- public static IRubyObject parse(ThreadContext context, IRubyObject self, IRubyObject arg) {
44
- Ruby ruby = context.getRuntime();
45
- try {
46
- Object o;
47
- if (arg instanceof RubyString) {
48
- o = mapper.readValue(arg.toString(), Object.class);
49
- } else if ((arg instanceof RubyIO) || (arg instanceof RubyStringIO)) {
50
- IRubyObject stream = IOJavaAddons.AnyIO.any_to_inputstream(context, arg);
51
- o = mapper.readValue((InputStream)stream.toJava(InputStream.class), Object.class);
52
- } else {
53
- throw ruby.newArgumentError("Unsupported source. This method accepts String or IO");
54
- }
55
- return (RubyObject)JavaUtil.convertJavaToRuby(ruby, o);
56
- }
57
- catch (JsonProcessingException e) {
58
- throw ParseError.newParseError(ruby, e.getLocalizedMessage());
59
- }
60
- catch (IOException e) {
61
- throw ruby.newIOError(e.getLocalizedMessage());
62
- }
63
- }
64
-
65
- @JRubyMethod(module = true, name = {"generate", "dump"}, required = 1)
66
- public static IRubyObject generate(ThreadContext context, IRubyObject self, IRubyObject arg) {
67
- Ruby ruby = context.getRuntime();
68
- Object obj = arg.toJava(Object.class);
69
- try {
70
- String s = mapper.writeValueAsString(obj);
71
- return ruby.newString(s);
72
- }
73
- catch (JsonProcessingException e) {
74
- throw ParseError.newParseError(ruby, e.getLocalizedMessage());
75
- }
76
- catch (IOException e) {
77
- throw ruby.newIOError(e.getLocalizedMessage());
78
- }
79
- }
80
- }