| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package net.sf.json.groovy; |
| 18 | |
|
| 19 | |
import groovy.lang.GroovyObjectSupport; |
| 20 | |
|
| 21 | |
import java.io.BufferedReader; |
| 22 | |
import java.io.File; |
| 23 | |
import java.io.FileReader; |
| 24 | |
import java.io.IOException; |
| 25 | |
import java.io.InputStream; |
| 26 | |
import java.io.InputStreamReader; |
| 27 | |
import java.io.Reader; |
| 28 | |
import java.net.URL; |
| 29 | |
|
| 30 | |
import net.sf.json.JSON; |
| 31 | |
import net.sf.json.JSONSerializer; |
| 32 | |
import net.sf.json.JsonConfig; |
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
public class JsonSlurper extends GroovyObjectSupport { |
| 40 | |
|
| 41 | |
private JsonConfig jsonConfig; |
| 42 | |
|
| 43 | |
public JsonSlurper() { |
| 44 | 0 | this( new JsonConfig() ); |
| 45 | 0 | } |
| 46 | |
|
| 47 | 0 | public JsonSlurper( JsonConfig jsonConfig ) { |
| 48 | 0 | this.jsonConfig = jsonConfig != null ? jsonConfig : new JsonConfig(); |
| 49 | 0 | } |
| 50 | |
|
| 51 | |
public JSON parse( File file ) throws IOException { |
| 52 | 0 | return parse( new FileReader( file ) ); |
| 53 | |
} |
| 54 | |
|
| 55 | |
public JSON parse( URL url ) throws IOException { |
| 56 | 0 | return parse( url.openConnection().getInputStream() ); |
| 57 | |
} |
| 58 | |
|
| 59 | |
public JSON parse( InputStream input ) throws IOException { |
| 60 | 0 | return parse( new InputStreamReader( input ) ); |
| 61 | |
} |
| 62 | |
|
| 63 | |
public JSON parse( String uri ) throws IOException { |
| 64 | 0 | return parse( new URL( uri ) ); |
| 65 | |
} |
| 66 | |
|
| 67 | |
public JSON parse( Reader reader ) throws IOException { |
| 68 | |
|
| 69 | |
|
| 70 | 0 | StringBuffer buffer = new StringBuffer(); |
| 71 | 0 | BufferedReader in = new BufferedReader( reader ); |
| 72 | 0 | String line = null; |
| 73 | 0 | while( (line = in.readLine()) != null ) { |
| 74 | 0 | buffer.append( line ).append( "\n" ); |
| 75 | |
} |
| 76 | 0 | return parseText( buffer.toString() ); |
| 77 | |
} |
| 78 | |
|
| 79 | |
public JSON parseText( String text ) { |
| 80 | 0 | return JSONSerializer.toJSON( text, jsonConfig ); |
| 81 | |
} |
| 82 | |
} |