Class: Puppetx::Puppetlabs::SyntaxCheckers::Json
- Inherits:
-
Puppetx::Puppet::SyntaxChecker
- Object
- Puppetx::Puppet::SyntaxChecker
- Puppetx::Puppetlabs::SyntaxCheckers::Json
- Defined in:
- lib/puppetx/puppetlabs/syntax_checkers/json.rb
Overview
A syntax checker for JSON.
Instance Method Summary collapse
-
#check(text, syntax, acceptor, location_info = {}) ⇒ Boolean
Checks the text for JSON syntax issues and reports them to the given acceptor.
Instance Method Details
#check(text, syntax, acceptor, location_info = {}) ⇒ Boolean
Checks the text for JSON syntax issues and reports them to the given acceptor. This implementation is abstract, it raises NotImplementedError since a subclass should have implemented the method.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/puppetx/puppetlabs/syntax_checkers/json.rb', line 17 def check(text, syntax, acceptor, location_info={}) raise ArgumentError.new("Json syntax checker: the text to check must be a String.") unless text.is_a?(String) raise ArgumentError.new("Json syntax checker: the syntax identifier must be a String, e.g. json, data+json") unless syntax.is_a?(String) raise ArgumentError.new("Json syntax checker: invalid Acceptor, got: '#{acceptor.class.name}'.") unless acceptor.is_a?(Puppet::Pops::Validation::Acceptor) raise ArgumentError.new("Json syntax checker: location_info must be a Hash") unless info.is_a?(Hash) begin JSON.parse(text) rescue => e # Cap the message to 100 chars and replace newlines msg = "Json syntax checker:: Cannot parse invalid JSON string. \"#{e.message().slice(0,100).gsub(/\r?\n/, "\\n")}\"" # TODO: improve the pops API to allow simpler diagnostic creation while still maintaining capabilities # and the issue code. (In this case especially, where there is only a single error message being issued). # issue = Puppet::Pops::Issues::issue(:ILLEGAL_JSON) { msg } source_pos = Puppet::Pops::Adapters::SourcePosAdapter.new() source_pos.line = location_info[:line] source_pos.pos = location_info[:pos] acceptor.accept(Puppet::Pops::Validation::Diagnostic.new(:error, issue, location_info[:file], source_pos, {})) end end |