rdf-json 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -1,19 +1,19 @@
1
- RDF::JSON: RDF/JSON Support for RDF.rb
2
- ======================================
1
+ RDF/JSON Support for RDF.rb
2
+ ===========================
3
3
 
4
- This is an [RDF.rb](http://rdf.rubyforge.org/) plugin that adds support for
5
- parsing/serializing the RDF/JSON serialization format.
4
+ This is an [RDF.rb][] plugin that adds support for parsing/serializing the
5
+ [RDF/JSON][] serialization format.
6
6
 
7
7
  * <http://github.com/bendiken/rdf-json>
8
8
 
9
- ### About RDF/JSON
10
-
11
- * <http://n2.talis.com/wiki/RDF_JSON_Specification>
12
-
13
9
  Documentation
14
10
  -------------
15
11
 
16
12
  * {RDF::JSON}
13
+ * {RDF::JSON::Extensions}
14
+ * {RDF::JSON::Format}
15
+ * {RDF::JSON::Reader}
16
+ * {RDF::JSON::Writer}
17
17
 
18
18
  Dependencies
19
19
  ------------
@@ -51,3 +51,6 @@ License
51
51
 
52
52
  `RDF::JSON` is free and unencumbered public domain software. For more
53
53
  information, see <http://unlicense.org/> or the accompanying UNLICENSE file.
54
+
55
+ [RDF.rb]: http://rdf.rubyforge.org/
56
+ [RDF/JSON]: http://n2.talis.com/wiki/RDF_JSON_Specification
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.0
1
+ 0.0.1
data/lib/rdf/json.rb CHANGED
@@ -4,39 +4,43 @@ module RDF
4
4
  ##
5
5
  # **`RDF::JSON`** is an RDF/JSON plugin for RDF.rb.
6
6
  #
7
- # Dependencies
8
- # ------------
9
- #
10
- # * [RDF.rb](http://gemcutter.org/gems/rdf) (>= 0.0.9)
11
- # * [JSON](http://gemcutter.org/gems/json_pure) (>= 1.2.0)
12
- #
13
- # Installation
14
- # ------------
15
- #
16
- # The recommended installation method is via RubyGems. To install the latest
17
- # official release from Gemcutter, do:
18
- #
19
- # % [sudo] gem install rdf-json
20
- #
21
- # Documentation
22
- # -------------
23
- #
24
- # * {RDF::JSON::Format}
25
- # * {RDF::JSON::Reader}
26
- # * {RDF::JSON::Writer}
27
- #
28
7
  # @example Requiring the `RDF::JSON` module
29
8
  # require 'rdf/json'
30
9
  #
10
+ # @example Serializing RDF values into RDF/JSON strings
11
+ # RDF::Node.new('foobar').to_json
12
+ # RDF::URI.new("http://rdf.rubyforge.org/").to_json
13
+ # RDF::Literal.new("Hello, world!").to_json
14
+ # RDF::Literal.new("Hello, world!", :language => 'en-US').to_json
15
+ # RDF::Literal.new(3.1415).to_json
16
+ # RDF::Literal.new('true', :datatype => RDF::XSD.boolean).to_json
17
+ # RDF::Statement.new(s, p, o).to_json
18
+ #
19
+ # @example Parsing RDF statements from an RDF/JSON file
20
+ # RDF::JSON::Reader.open("spec/data/test.json") do |reader|
21
+ # reader.each_statement do |statement|
22
+ # puts statement.inspect
23
+ # end
24
+ # end
25
+ #
26
+ # @example Serializing RDF statements into an RDF/JSON file
27
+ # RDF::JSON::Writer.open("spec/data/test.json") do |writer|
28
+ # graph.each_statement do |statement|
29
+ # writer << statement
30
+ # end
31
+ # end
32
+ #
31
33
  # @see http://rdf.rubyforge.org/
32
34
  # @see http://n2.talis.com/wiki/RDF_JSON_Specification
33
35
  # @see http://en.wikipedia.org/wiki/JSON
34
36
  #
35
37
  # @author [Arto Bendiken](http://ar.to/)
36
38
  module JSON
39
+ require 'json'
40
+ require 'rdf/json/extensions'
37
41
  require 'rdf/json/format'
38
42
  autoload :Reader, 'rdf/json/reader'
39
43
  autoload :Writer, 'rdf/json/writer'
40
44
  autoload :VERSION, 'rdf/json/version'
41
- end
42
- end
45
+ end # module JSON
46
+ end # module RDF
@@ -0,0 +1,137 @@
1
+ module RDF::JSON
2
+ ##
3
+ # RDF/JSON extensions for [RDF.rb](http://rdf.rubyforge.org/) core classes
4
+ # and mixins.
5
+ #
6
+ # Classes are extended with two new instance methods:
7
+ #
8
+ # * `#to_rdf_json` returns the RDF/JSON representation as a `Hash` object.
9
+ # * `#to_json` returns the serialized RDF/JSON representation as a string.
10
+ #
11
+ # @example Serializing blank nodes into RDF/JSON format
12
+ # RDF::Node.new(id).to_json
13
+ #
14
+ # @example Serializing URI references into RDF/JSON format
15
+ # RDF::URI.new("http://rdf.rubyforge.org/").to_json
16
+ #
17
+ # @example Serializing plain literals into RDF/JSON format
18
+ # RDF::Literal.new("Hello, world!").to_json
19
+ #
20
+ # @example Serializing language-tagged literals into RDF/JSON format
21
+ # RDF::Literal.new("Hello, world!", :language => 'en-US').to_json
22
+ #
23
+ # @example Serializing datatyped literals into RDF/JSON format
24
+ # RDF::Literal.new(3.1415).to_json
25
+ # RDF::Literal.new('true', :datatype => RDF::XSD.boolean).to_json
26
+ #
27
+ # @example Serializing statements into RDF/JSON format
28
+ # RDF::Statement.new(s, p, o).to_json
29
+ #
30
+ # @example Serializing enumerables into RDF/JSON format
31
+ # [RDF::Statement.new(s, p, o)].extend(RDF::Enumerable).to_json
32
+ #
33
+ module Extensions
34
+ ##
35
+ # RDF/JSON extensions for `RDF::Value`.
36
+ module Value
37
+ ##
38
+ # Returns the serialized RDF/JSON representation of this value.
39
+ #
40
+ # @return [String]
41
+ def to_json
42
+ # Any RDF/JSON-compatible class must implement `#to_rdf_json`:
43
+ to_rdf_json.to_json
44
+ end
45
+ end
46
+
47
+ ##
48
+ # RDF/JSON extensions for `RDF::Node`.
49
+ module Node
50
+ ##
51
+ # Returns the RDF/JSON representation of this blank node.
52
+ #
53
+ # @return [Hash]
54
+ def to_rdf_json
55
+ {:type => :bnode, :value => to_s}
56
+ end
57
+ end
58
+
59
+ ##
60
+ # RDF/JSON extensions for `RDF::URI`.
61
+ module URI
62
+ ##
63
+ # Returns the RDF/JSON representation of this URI reference.
64
+ #
65
+ # @return [Hash]
66
+ def to_rdf_json
67
+ {:type => :uri, :value => to_s}
68
+ end
69
+ end
70
+
71
+ ##
72
+ # RDF/JSON extensions for `RDF::Literal`.
73
+ module Literal
74
+ ##
75
+ # Returns the RDF/JSON representation of this literal.
76
+ #
77
+ # @return [Hash]
78
+ def to_rdf_json
79
+ case
80
+ when datatype? # FIXME: use `has_datatype?` in RDF.rb 0.1.0
81
+ {:type => :literal, :value => value.to_s, :datatype => datatype.to_s}
82
+ when language? # FIXME: use `has_language?` in RDF.rb 0.1.0
83
+ {:type => :literal, :value => value.to_s, :lang => language.to_s}
84
+ else
85
+ {:type => :literal, :value => value.to_s}
86
+ end
87
+ end
88
+ end
89
+
90
+ ##
91
+ # RDF/JSON extensions for `RDF::Statement`.
92
+ module Statement
93
+ ##
94
+ # Returns the RDF/JSON representation of this statement.
95
+ #
96
+ # @return [Hash]
97
+ def to_rdf_json
98
+ # FIXME: improve the RDF::Statement constructor in RDF.rb 0.1.0
99
+ s, p, o = subject.to_s, predicate.to_s, object.is_a?(RDF::Value) ? object : RDF::Literal.new(object)
100
+ {s => {p => [o.to_rdf_json]}}
101
+ end
102
+ end
103
+
104
+ ##
105
+ # RDF/JSON extensions for `RDF::Enumerable`.
106
+ module Enumerable
107
+ ##
108
+ # Returns the serialized RDF/JSON representation of this object.
109
+ #
110
+ # @return [String]
111
+ def to_json
112
+ to_rdf_json.to_json
113
+ end
114
+
115
+ ##
116
+ # Returns the RDF/JSON representation of this object.
117
+ #
118
+ # @return [Hash]
119
+ def to_rdf_json
120
+ json = {}
121
+ each_statement do |statement|
122
+ s = statement.subject.to_s
123
+ p = statement.predicate.to_s
124
+ o = statement.object.is_a?(RDF::Value) ? statement.object : RDF::Literal.new(statement.object)
125
+ json[s] ||= {}
126
+ json[s][p] ||= []
127
+ json[s][p] << o.to_rdf_json
128
+ end
129
+ json
130
+ end
131
+ end
132
+ end # module Extensions
133
+
134
+ Extensions.constants.each do |klass|
135
+ RDF.const_get(klass).send(:include, Extensions.const_get(klass))
136
+ end
137
+ end # module RDF::JSON
@@ -2,6 +2,13 @@ module RDF::JSON
2
2
  ##
3
3
  # RDF/JSON format specification.
4
4
  #
5
+ # @example Obtaining an RDF/JSON format class
6
+ # RDF::Format.for(:json) #=> RDF::JSON::Format
7
+ # RDF::Format.for("spec/data/test.json")
8
+ # RDF::Format.for(:file_name => "spec/data/test.json")
9
+ # RDF::Format.for(:file_extension => "json")
10
+ # RDF::Format.for(:content_type => "application/json")
11
+ #
5
12
  # @see http://n2.talis.com/wiki/RDF_JSON_Specification
6
13
  class Format < RDF::Format
7
14
  content_type 'application/json', :extension => :json
@@ -11,5 +18,5 @@ module RDF::JSON
11
18
  writer { RDF::JSON::Writer }
12
19
 
13
20
  require 'json'
14
- end
15
- end
21
+ end # class Format
22
+ end # module RDF::JSON
@@ -2,17 +2,32 @@ module RDF::JSON
2
2
  ##
3
3
  # RDF/JSON parser.
4
4
  #
5
- # @example Reading RDF/JSON data
5
+ # @example Obtaining an RDF/JSON reader class
6
+ # RDF::Reader.for(:json) #=> RDF::JSON::Reader
7
+ # RDF::Reader.for("spec/data/test.json")
8
+ # RDF::Reader.for(:file_name => "spec/data/test.json")
9
+ # RDF::Reader.for(:file_extension => "json")
10
+ # RDF::Reader.for(:content_type => "application/json")
11
+ #
12
+ # @example Parsing RDF statements from an RDF/JSON file
6
13
  # RDF::JSON::Reader.open("spec/data/test.json") do |reader|
7
14
  # reader.each_statement do |statement|
8
15
  # puts statement.inspect
9
16
  # end
10
17
  # end
11
18
  #
19
+ # @example Parsing RDF statements from an RDF/JSON string
20
+ # data = StringIO.new(File.read("spec/data/test.json"))
21
+ # RDF::JSON::Reader.new(data) do |reader|
22
+ # reader.each_statement do |statement|
23
+ # puts statement.inspect
24
+ # end
25
+ # end
26
+ #
12
27
  # @see http://n2.talis.com/wiki/RDF_JSON_Specification
13
28
  class Reader < RDF::Reader
14
29
  format RDF::JSON::Format
15
30
 
16
31
  # TODO
17
- end
18
- end
32
+ end # class Reader
33
+ end # module RDF::JSON
@@ -2,7 +2,7 @@ module RDF module JSON
2
2
  module VERSION
3
3
  MAJOR = 0
4
4
  MINOR = 0
5
- TINY = 0
5
+ TINY = 1
6
6
  EXTRA = nil
7
7
 
8
8
  STRING = [MAJOR, MINOR, TINY].join('.')
@@ -15,5 +15,9 @@ module RDF module JSON
15
15
  ##
16
16
  # @return [String]
17
17
  def self.to_str() STRING end
18
+
19
+ ##
20
+ # @return [Array(Integer, Integer, Integer)]
21
+ def self.to_a() [MAJOR, MINOR, TINY] end
18
22
  end
19
23
  end end
@@ -2,10 +2,89 @@ module RDF::JSON
2
2
  ##
3
3
  # RDF/JSON serializer.
4
4
  #
5
+ # @example Obtaining an RDF/JSON writer class
6
+ # RDF::Writer.for(:json) #=> RDF::JSON::Writer
7
+ # RDF::Writer.for("spec/data/test.json")
8
+ # RDF::Writer.for(:file_name => "spec/data/test.json")
9
+ # RDF::Writer.for(:file_extension => "json")
10
+ # RDF::Writer.for(:content_type => "application/json")
11
+ #
12
+ # @example Serializing RDF statements into an RDF/JSON file
13
+ # RDF::JSON::Writer.open("spec/data/test.json") do |writer|
14
+ # graph.each_statement do |statement|
15
+ # writer << statement
16
+ # end
17
+ # end
18
+ #
19
+ # @example Serializing RDF statements into an RDF/JSON string
20
+ # RDF::JSON::Writer.buffer do |writer|
21
+ # graph.each_statement do |statement|
22
+ # writer << statement
23
+ # end
24
+ # end
25
+ #
5
26
  # @see http://n2.talis.com/wiki/RDF_JSON_Specification
6
27
  class Writer < RDF::Writer
7
28
  format RDF::JSON::Format
8
29
 
9
- # TODO
10
- end
11
- end
30
+ ##
31
+ # Stores the RDF/JSON representation of a triple.
32
+ #
33
+ # @param [RDF::Resource] subject
34
+ # @param [RDF::URI] predicate
35
+ # @param [RDF::Value] object
36
+ # @return [void]
37
+ # @see #write_epilogue
38
+ def write_triple(subject, predicate, object)
39
+ s = subject.to_s
40
+ p = predicate.to_s
41
+ o = object.is_a?(RDF::Value) ? object : RDF::Literal.new(object)
42
+ @json ||= {}
43
+ @json[s] ||= {}
44
+ @json[s][p] ||= []
45
+ @json[s][p] << o.to_rdf_json
46
+ end
47
+
48
+ ##
49
+ # Outputs the RDF/JSON representation of all stored triples.
50
+ #
51
+ # @return [void]
52
+ # @see #write_triple
53
+ def write_epilogue
54
+ puts @json.to_json
55
+ end
56
+
57
+ ##
58
+ # Returns the RDF/JSON representation of a blank node.
59
+ #
60
+ # @param [RDF::Node] value
61
+ # @param [Hash{Symbol => Object}] options
62
+ # @return [String]
63
+ def format_node(value, options = {})
64
+ value.to_json
65
+ end
66
+
67
+ ##
68
+ # Returns the RDF/JSON representation of a URI reference.
69
+ #
70
+ # @param [RDF::URI] value
71
+ # @param [Hash{Symbol => Object}] options
72
+ # @return [String]
73
+ def format_uri(value, options = {})
74
+ value.to_json
75
+ end
76
+
77
+ ##
78
+ # Returns the RDF/JSON representation of a literal.
79
+ #
80
+ # @param [RDF::Literal, String, #to_s] value
81
+ # @param [Hash{Symbol => Object}] options
82
+ # @return [String]
83
+ def format_literal(value, options = {})
84
+ case value
85
+ when RDF::Literal then value.to_json
86
+ else RDF::Literal.new(value).to_json
87
+ end
88
+ end
89
+ end # class Writer
90
+ end # module RDF::JSON
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rdf-json
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arto Bendiken
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-28 00:00:00 +01:00
12
+ date: 2010-02-02 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -30,7 +30,7 @@ dependencies:
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 0.5.2
33
+ version: 0.5.3
34
34
  version:
35
35
  - !ruby/object:Gem::Dependency
36
36
  name: rdf
@@ -65,6 +65,7 @@ files:
65
65
  - README
66
66
  - UNLICENSE
67
67
  - VERSION
68
+ - lib/rdf/json/extensions.rb
68
69
  - lib/rdf/json/format.rb
69
70
  - lib/rdf/json/reader.rb
70
71
  - lib/rdf/json/version.rb