xml2json-rb 0.1.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Cargo.lock +1 -1
- data/ext/xml2json/Cargo.toml +1 -1
- data/ext/xml2json/src/lib.rs +172 -33
- data/lib/xml2json/version.rb +1 -1
- data/lib/xml2json/xml2json.so +0 -0
- data/lib/xml2json.rb +27 -2
- data/sig/xml2_json/json.rbs +5 -0
- data/sig/xml2_json/xml/encoding.rbs +7 -0
- data/sig/xml2_json/xml/version.rbs +8 -0
- data/sig/xml2_json/xml.rbs +5 -0
- metadata +92 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 894ff6fe88c1e4a0e7bbb426419954f98413f568f8ac361784e60537074033fd
|
4
|
+
data.tar.gz: face561e86ac59fb767535689377e361f171574b77722ba55a55056c96630767
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2d3917434cd6824082cb8256ef752e10af0153d29da3b00365f0ed5ceebcef5626e422998b52a7f1a3c63b44ab2b49586f079557e1cb4888a08cf2d4f128c9bc
|
7
|
+
data.tar.gz: 36cf6993a5ca3f586357b8d46b9a038ad53cab83eae759fa1506ab58989cdd3c97dad69fe15a44f3ecea49eb3ac7287f35a75a01582e3bd13d493e58029a523a
|
data/Cargo.lock
CHANGED
data/ext/xml2json/Cargo.toml
CHANGED
data/ext/xml2json/src/lib.rs
CHANGED
@@ -1,47 +1,182 @@
|
|
1
|
-
use magnus::{define_module, function, prelude::*, Error};
|
2
|
-
use
|
1
|
+
use magnus::{define_module, function, prelude::*, Error, Value};
|
2
|
+
use magnus::scan_args::{scan_args};
|
3
|
+
use xml2json_rs::{XmlBuilder, JsonBuilder, JsonConfig, XmlConfig, Declaration, Version, Encoding, Indentation};
|
3
4
|
|
4
5
|
// fn hello(subject: String) -> String {
|
5
6
|
// format!("Hello from Rust, {}!", subject)
|
6
7
|
// }
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
9
|
+
#[macro_export]
|
10
|
+
macro_rules! set_arg {
|
11
|
+
($config:expr, $opts_hash:expr, $arg:ident, $arg_type:ident) => (
|
12
|
+
let arg_value = $opts_hash.lookup::<_, Option<$arg_type>>(magnus::Symbol::new(stringify!($arg)))?;
|
13
|
+
if arg_value.is_some() {
|
14
|
+
$config.$arg(arg_value.unwrap());
|
15
|
+
}
|
16
|
+
);
|
17
|
+
}
|
11
18
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
// self
|
16
|
-
// }
|
17
|
-
//
|
18
|
-
// fn build(self, json_s: String) -> Result<String, Error> {
|
19
|
-
// build_xml_impl(json_s, Some(self))
|
20
|
-
// }
|
21
|
-
// }
|
19
|
+
fn map_xml2json_err(error: xml2json_rs::X2JError) -> Error {
|
20
|
+
Error::new(magnus::exception::runtime_error(), error.details())
|
21
|
+
}
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
|
23
|
+
fn build_xml(args: &[Value]) -> Result<String, Error> {
|
24
|
+
let args = scan_args::<_, _, (), (), _, ()>(args)?;
|
25
|
+
let (json_s, ): (String, ) = args.required;
|
26
|
+
let (opts, ): (Option<magnus::RHash>, ) = args.optional;
|
27
|
+
let _: () = args.keywords;
|
26
28
|
|
27
|
-
|
28
|
-
//
|
29
|
-
//
|
30
|
-
|
31
|
-
|
32
|
-
|
29
|
+
let mut xml_builder: XmlBuilder;
|
30
|
+
if opts.is_some() { // yep, even if it's an empty hash
|
31
|
+
// println!("{}", opts.unwrap().to_string());
|
32
|
+
let opts_hash = opts.unwrap();
|
33
|
+
let mut config = XmlConfig::new();
|
34
|
+
set_arg!(config, opts_hash, root_name, String);
|
35
|
+
set_arg!(config, opts_hash, attrkey, String);
|
36
|
+
set_arg!(config, opts_hash, charkey, String);
|
37
|
+
|
38
|
+
let decl_version = opts_hash.lookup::<_, Option<Value>>(magnus::Symbol::new("version"))?;
|
39
|
+
let decl_encoding = opts_hash.lookup::<_, Option<Value>>(magnus::Symbol::new("encoding"))?;
|
40
|
+
let decl_standalone = opts_hash.lookup::<_, Option<bool>>(magnus::Symbol::new("standalone"))?;
|
41
|
+
if decl_version.is_some()
|
42
|
+
|| decl_encoding.is_some()
|
43
|
+
|| decl_standalone.is_some()
|
44
|
+
{ // something is specified
|
45
|
+
// I didn't find a way to get defaults without copying them
|
46
|
+
let decl_version_val;
|
47
|
+
if decl_version.is_some() {
|
48
|
+
let decl_version_str = unsafe { decl_version.unwrap().to_s() }?.into_owned();
|
49
|
+
decl_version_val = Version::try_from(decl_version_str.as_str())
|
50
|
+
.map_err(map_xml2json_err)?;
|
51
|
+
} else { decl_version_val = Version::XML10; }
|
52
|
+
let decl_encoding_val;
|
53
|
+
if decl_encoding.is_some() {
|
54
|
+
let decl_encoding_str = unsafe { decl_encoding.unwrap().to_s() }?.into_owned();
|
55
|
+
decl_encoding_val = Some(Encoding::try_from(decl_encoding_str.as_str())
|
56
|
+
.map_err(map_xml2json_err)?);
|
57
|
+
} else { decl_encoding_val = None; }
|
58
|
+
|
59
|
+
let decl = Declaration::new(
|
60
|
+
decl_version_val,
|
61
|
+
decl_encoding_val,
|
62
|
+
decl_standalone,
|
63
|
+
);
|
64
|
+
config.decl(decl);
|
65
|
+
}
|
66
|
+
|
67
|
+
let indent = opts_hash.lookup::<_, Option<bool>>(magnus::Symbol::new("indent"))?;
|
68
|
+
if indent.unwrap_or(true) {
|
69
|
+
let indent_char = opts_hash.lookup::<_, Option<char>>(magnus::Symbol::new("indent_char"))?;
|
70
|
+
let indent_size = opts_hash.lookup::<_, Option<usize>>(magnus::Symbol::new("indent_size"))?;
|
71
|
+
if indent_char.is_some()
|
72
|
+
|| indent_size.is_some()
|
73
|
+
{
|
74
|
+
let indent_char_val: u8;
|
75
|
+
if indent_char.is_some() {
|
76
|
+
indent_char_val = u8::try_from(indent_char.unwrap()).map_err(|error| Error::new(magnus::exception::type_error(), error.to_string()))?;
|
77
|
+
} else { indent_char_val = b' '; }
|
78
|
+
|
79
|
+
let intent = Indentation::new(
|
80
|
+
indent_char_val,
|
81
|
+
indent_size.unwrap_or(2),
|
82
|
+
);
|
83
|
+
config.rendering(intent);
|
84
|
+
} else if indent.unwrap_or(false) {
|
85
|
+
// because Indentation::default is not the same as not calling config.rendering at all;
|
86
|
+
config.rendering(Indentation::default());
|
87
|
+
}
|
88
|
+
}
|
89
|
+
xml_builder = config.finalize();
|
90
|
+
} else { xml_builder = XmlBuilder::default(); }
|
33
91
|
|
34
|
-
fn build_xml(json_s: String) -> Result<String, Error> {
|
35
|
-
let mut xml_builder = XmlBuilder::default();
|
36
92
|
xml_builder.build_from_json_string(&json_s).or_else(|error| {
|
37
|
-
Err(
|
93
|
+
Err(map_xml2json_err(error))
|
38
94
|
})
|
39
95
|
}
|
40
96
|
|
41
|
-
fn build_json(
|
42
|
-
|
97
|
+
fn build_json(args: &[Value]) -> Result<String, Error> {
|
98
|
+
// https://docs.rs/magnus/latest/magnus/scan_args/fn.scan_args.html
|
99
|
+
let args = scan_args::<_, _, (), (), _, ()>(args)?;
|
100
|
+
let (xml, ): (String, ) = args.required;
|
101
|
+
let (opts, ): (Option<magnus::RHash>, ) = args.optional;
|
102
|
+
// let _: () = args.optional;
|
103
|
+
let _: () = args.keywords;
|
104
|
+
|
105
|
+
// Impossible, too long
|
106
|
+
// let kw = get_kwargs::<_, (), (
|
107
|
+
// Option<String>, // charkey
|
108
|
+
// Option<String>, // attrkey
|
109
|
+
// Option<String>, // empty_tag
|
110
|
+
// Option<bool>, // explicit_root
|
111
|
+
// Option<bool>, // trim
|
112
|
+
// Option<bool>, // ignore_attrs
|
113
|
+
// Option<bool>, // merge_attrs
|
114
|
+
// Option<bool>, // normalize_text
|
115
|
+
// Option<bool>, // lowercase_tags
|
116
|
+
// Option<bool>, // explicit_array
|
117
|
+
// Option<bool>, // explicit_charkey
|
118
|
+
// ), ()>(args.keywords, &[], &[
|
119
|
+
// "charkey",
|
120
|
+
// "attrkey",
|
121
|
+
// "empty_tag",
|
122
|
+
// "explicit_root",
|
123
|
+
// "trim",
|
124
|
+
// "ignore_attrs",
|
125
|
+
// "merge_attrs",
|
126
|
+
// "normalize_text",
|
127
|
+
// "lowercase_tags",
|
128
|
+
// "explicit_array",
|
129
|
+
// "explicit_charkey",
|
130
|
+
// ])?;
|
131
|
+
// let (
|
132
|
+
// charkey,
|
133
|
+
// attrkey,
|
134
|
+
// empty_tag,
|
135
|
+
// explicit_root,
|
136
|
+
// trim,
|
137
|
+
// ignore_attrs,
|
138
|
+
// merge_attrs,
|
139
|
+
// normalize_text,
|
140
|
+
// lowercase_tags,
|
141
|
+
// explicit_array,
|
142
|
+
// explicit_charkey,
|
143
|
+
// ) = kw.optional;
|
144
|
+
// if charkey.is_some()
|
145
|
+
// || attrkey.is_some()
|
146
|
+
// || empty_tag.is_some()
|
147
|
+
// || explicit_root.is_some()
|
148
|
+
// || trim.is_some()
|
149
|
+
// || ignore_attrs.is_some()
|
150
|
+
// || merge_attrs.is_some()
|
151
|
+
// || normalize_text.is_some()
|
152
|
+
// || lowercase_tags.is_some()
|
153
|
+
// || explicit_array.is_some()
|
154
|
+
// || explicit_charkey.is_some()
|
155
|
+
// {
|
156
|
+
// println!("any")
|
157
|
+
// } else { println!("none") }
|
158
|
+
|
159
|
+
let json_builder: JsonBuilder;
|
160
|
+
if opts.is_some() { // yep, even if it's an empty hash
|
161
|
+
// println!("{}", opts.unwrap().to_string());
|
162
|
+
let opts_hash = opts.unwrap();
|
163
|
+
let mut config = JsonConfig::new();
|
164
|
+
set_arg!(config, opts_hash, charkey, String);
|
165
|
+
set_arg!(config, opts_hash, attrkey, String);
|
166
|
+
set_arg!(config, opts_hash, empty_tag, String);
|
167
|
+
set_arg!(config, opts_hash, explicit_root, bool);
|
168
|
+
set_arg!(config, opts_hash, trim, bool);
|
169
|
+
set_arg!(config, opts_hash, ignore_attrs, bool);
|
170
|
+
set_arg!(config, opts_hash, merge_attrs, bool);
|
171
|
+
set_arg!(config, opts_hash, normalize_text, bool);
|
172
|
+
set_arg!(config, opts_hash, lowercase_tags, bool);
|
173
|
+
set_arg!(config, opts_hash, explicit_array, bool);
|
174
|
+
set_arg!(config, opts_hash, explicit_charkey, bool);
|
175
|
+
json_builder = config.finalize();
|
176
|
+
} else { json_builder = JsonBuilder::default(); }
|
177
|
+
|
43
178
|
json_builder.build_string_from_xml(&xml).or_else(|error| {
|
44
|
-
Err(
|
179
|
+
Err(map_xml2json_err(error))
|
45
180
|
})
|
46
181
|
}
|
47
182
|
|
@@ -51,9 +186,13 @@ fn init() -> Result<(), Error> {
|
|
51
186
|
// module.define_singleton_method("hello", function!(hello, 1))?;
|
52
187
|
// It's not possible to wrap XmlBuilder
|
53
188
|
let xml = module.define_module("Xml")?;
|
54
|
-
xml.
|
55
|
-
|
189
|
+
let xml_version = xml.define_module("Version")?;
|
190
|
+
xml_version.const_set("XML10", Version::XML10.to_string())?;
|
191
|
+
xml_version.const_set("XML11", Version::XML11.to_string())?;
|
192
|
+
let xml_encoding = xml.define_module("Encoding")?;
|
193
|
+
xml_encoding.const_set("UTF8", Encoding::UTF8.to_string())?;
|
194
|
+
xml.define_singleton_method("build", function!(build_xml, -1))?;
|
56
195
|
let json = module.define_module("Json")?;
|
57
|
-
json.define_singleton_method("build", function!(build_json, 1))?;
|
196
|
+
json.define_singleton_method("build", function!(build_json, -1))?;
|
58
197
|
Ok(())
|
59
198
|
}
|
data/lib/xml2json/version.rb
CHANGED
data/lib/xml2json/xml2json.so
CHANGED
Binary file
|
data/lib/xml2json.rb
CHANGED
@@ -3,7 +3,32 @@
|
|
3
3
|
require_relative "xml2json/version"
|
4
4
|
require_relative "xml2json/xml2json"
|
5
5
|
|
6
|
+
# @see https://github.com/novcn/xml2json-rs docs for the wrapped library
|
6
7
|
module Xml2Json
|
7
|
-
|
8
|
-
#
|
8
|
+
# @!parse [ruby]
|
9
|
+
# module Xml
|
10
|
+
# module Version
|
11
|
+
# XML10 = "1.0"
|
12
|
+
# XML11 = "1.1"
|
13
|
+
# end
|
14
|
+
# module Encoding
|
15
|
+
# UTF8 = "UTF-8"
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# # @param json_s [String, #to_str] JSON string
|
19
|
+
# # @param opts [Hash<Symbol, Object>] config params
|
20
|
+
# # @see https://docs.rs/xml2json-rs/1.0.1/xml2json_rs/struct.XmlConfig.html rust doc
|
21
|
+
# # @return [String] XML string
|
22
|
+
# # @raise [RuntimeError] if the input string is invalid
|
23
|
+
# def self.build(json_s, opts: nil); end
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# module Json
|
27
|
+
# # @param xml [String, #to_str] XML string
|
28
|
+
# # @param opts [Hash<Symbol, Object>] config params
|
29
|
+
# # @see https://docs.rs/xml2json-rs/1.0.1/xml2json_rs/struct.JsonConfig.html rust doc
|
30
|
+
# # @return [String] JSON string
|
31
|
+
# # @raise [RuntimeError] if the input string is invalid
|
32
|
+
# def self.build(xml, opts: nil); end
|
33
|
+
# end
|
9
34
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xml2json-rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- uvlad7
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-07-
|
11
|
+
date: 2023-07-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: yard-rustdoc
|
@@ -24,7 +24,91 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
-
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pry
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry-byebug
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: yard
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rack
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rackup
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: puma
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: This gem helps to convert xml strings to json and vise versa
|
28
112
|
email:
|
29
113
|
- uvlad7@gmail.com
|
30
114
|
executables: []
|
@@ -40,6 +124,10 @@ files:
|
|
40
124
|
- lib/xml2json.rb
|
41
125
|
- lib/xml2json/version.rb
|
42
126
|
- lib/xml2json/xml2json.so
|
127
|
+
- sig/xml2_json/json.rbs
|
128
|
+
- sig/xml2_json/xml.rbs
|
129
|
+
- sig/xml2_json/xml/encoding.rbs
|
130
|
+
- sig/xml2_json/xml/version.rbs
|
43
131
|
- sig/xml2json.rbs
|
44
132
|
homepage: https://github.com/uvlad7/xml2json-rb
|
45
133
|
licenses:
|
@@ -48,6 +136,7 @@ metadata:
|
|
48
136
|
homepage_uri: https://github.com/uvlad7/xml2json-rb
|
49
137
|
source_code_uri: https://github.com/uvlad7/xml2json-rb
|
50
138
|
changelog_uri: https://github.com/uvlad7/xml2json-rb/blob/main/CHANGELOG.md
|
139
|
+
documentation_uri: https://rubydoc.info/gems/xml2json-rb/0.2.1
|
51
140
|
post_install_message:
|
52
141
|
rdoc_options: []
|
53
142
|
require_paths:
|