json-schema-reader 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/bin/jsontree +12 -0
- data/lib/json-schema-reader.rb +134 -0
- metadata +46 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: df484979b652b652e8dd16d12951edb83de6ec1d
|
4
|
+
data.tar.gz: e4fc53bc4f9d7c7767e5d0aabcdf1e273071e13e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0ea857ff9aecd4d7c0623f7c7e701ee193585a19df33fd779c5b1d84ca707a1ca56c64d288c2254e85df06d0486095f7538e2d79510aa2f1b0c034a3314c7bbc
|
7
|
+
data.tar.gz: e2c99940ec7aee65cba53193ee29dd226aef24a01d5190591d5e9074635863ca314a36d3b53a43af0031417fc768294ee9e71d9b481af2645d5bd5d939869ddc
|
data/bin/jsontree
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'json-schema-reader'
|
4
|
+
|
5
|
+
if ARGV.count == 0
|
6
|
+
puts "jsontree prints a tree view of a JSON schema file\n\n"
|
7
|
+
puts " Usage:\n jsontree path/to/file.json\n\n"
|
8
|
+
exit 1
|
9
|
+
end
|
10
|
+
|
11
|
+
schema = Schema.load_from_file ARGV[0]
|
12
|
+
schema.pretty_generate
|
@@ -0,0 +1,134 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'jsonpath'
|
3
|
+
require 'pathname'
|
4
|
+
|
5
|
+
# A little hack to let us reference hash members with dot notation
|
6
|
+
class Hash
|
7
|
+
def method_missing sym
|
8
|
+
self[sym.to_s]
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class Schema
|
13
|
+
attr_accessor :data, :uri
|
14
|
+
|
15
|
+
def initialize data, uri
|
16
|
+
@data = JSON.parse data
|
17
|
+
@uri = uri
|
18
|
+
@data = expand @data
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.load_from_file path
|
22
|
+
Schema.new File.read(path), Pathname.new(path).realdirpath
|
23
|
+
end
|
24
|
+
|
25
|
+
def path
|
26
|
+
@uri.dirname
|
27
|
+
end
|
28
|
+
|
29
|
+
def title
|
30
|
+
@data.title
|
31
|
+
end
|
32
|
+
|
33
|
+
def pretty_generate
|
34
|
+
puts @data.title
|
35
|
+
print_line @data
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def print_line line, indent = ''
|
41
|
+
if line.properties && line.patternProperties
|
42
|
+
pp = line.properties.merge line.patternProperties
|
43
|
+
else
|
44
|
+
pp = line.properties || line.patternProperties
|
45
|
+
end
|
46
|
+
|
47
|
+
pp.each_with_index do |(prop_k, prop_v), idx|
|
48
|
+
i = indent
|
49
|
+
|
50
|
+
if idx + 1 == pp.count
|
51
|
+
c = "\u2514"
|
52
|
+
s = " "
|
53
|
+
else
|
54
|
+
c = "\u251C"
|
55
|
+
s = "\u2502 "
|
56
|
+
end
|
57
|
+
|
58
|
+
print i + c
|
59
|
+
|
60
|
+
if (prop_v.required == true) || (line.required && line.required.is_a?(Array) && line.required.include?(prop_k))
|
61
|
+
print '!'
|
62
|
+
end
|
63
|
+
|
64
|
+
print prop_k
|
65
|
+
|
66
|
+
if prop_v.type
|
67
|
+
print ':' + prop_v.type
|
68
|
+
end
|
69
|
+
|
70
|
+
if prop_v.units
|
71
|
+
print ':' + prop_v.units
|
72
|
+
end
|
73
|
+
|
74
|
+
print "\n"
|
75
|
+
|
76
|
+
print_line prop_v, indent + s
|
77
|
+
end unless pp.nil?
|
78
|
+
end
|
79
|
+
|
80
|
+
def expand data
|
81
|
+
if data.definitions
|
82
|
+
data.definitions.each do |k, v|
|
83
|
+
if v['$ref']
|
84
|
+
v.merge! expand_ref v['$ref']
|
85
|
+
v.delete '$ref'
|
86
|
+
end
|
87
|
+
v = expand v
|
88
|
+
data.definitions[k] = v
|
89
|
+
end
|
90
|
+
end
|
91
|
+
if data.properties
|
92
|
+
data.properties.each do |k, v|
|
93
|
+
if v['$ref']
|
94
|
+
v.merge! expand_ref v['$ref']
|
95
|
+
v.delete '$ref'
|
96
|
+
end
|
97
|
+
v = expand v
|
98
|
+
data.properties[k] = v
|
99
|
+
end
|
100
|
+
end
|
101
|
+
if data.patternProperties
|
102
|
+
data.patternProperties.each do |k, v|
|
103
|
+
if v['$ref']
|
104
|
+
v.merge! expand_ref v['$ref']
|
105
|
+
v.delete '$ref'
|
106
|
+
end
|
107
|
+
v = expand v
|
108
|
+
data.patternProperties[k] = v
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
data
|
113
|
+
end
|
114
|
+
|
115
|
+
def expand_ref ref
|
116
|
+
if ref[0] == '#' # refers to current schema
|
117
|
+
path = ref.gsub(/[\/#]/, '.')
|
118
|
+
data = JsonPath.new(path).first @data
|
119
|
+
else
|
120
|
+
uri = (self.path + Pathname.new(ref)).to_s
|
121
|
+
tok = uri.split '#'
|
122
|
+
t_schema = Schema.load_from_file tok[0]
|
123
|
+
|
124
|
+
if tok[1]
|
125
|
+
path = tok[1].gsub(/\//, '.')
|
126
|
+
data = JsonPath.new(path).first t_schema.data
|
127
|
+
else
|
128
|
+
data = t_schema.data
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
data || {}
|
133
|
+
end
|
134
|
+
end
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: json-schema-reader
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tim Mathews
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-05 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Currently provides one function, printing a tree view of a schema
|
14
|
+
email: tim@signalk.org
|
15
|
+
executables:
|
16
|
+
- jsontree
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- bin/jsontree
|
21
|
+
- lib/json-schema-reader.rb
|
22
|
+
homepage: https://github.com/timmathews/json-schema-reader
|
23
|
+
licenses:
|
24
|
+
- GPLv3
|
25
|
+
metadata: {}
|
26
|
+
post_install_message:
|
27
|
+
rdoc_options: []
|
28
|
+
require_paths:
|
29
|
+
- lib
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 2.2.2
|
43
|
+
signing_key:
|
44
|
+
specification_version: 4
|
45
|
+
summary: Pretty print JSON Schemas
|
46
|
+
test_files: []
|