Commit 1661c717 authored by Robert May's avatar Robert May

Add a Gitlab::Json wrapper

Implements a GitLab JSON wrapper to allow deprecated usage of the
JSON gem, which allows us to upgrade the gem to a more modern
version.
parent 59056421
# frozen_string_literal: true
module Gitlab
module Json
class << self
def parse(*args)
adapter.parse(*args)
end
def parse!(*args)
adapter.parse!(*args)
end
def dump(*args)
adapter.dump(*args)
end
def generate(*args)
adapter.generate(*args)
end
def pretty_generate(*args)
adapter.pretty_generate(*args)
end
private
def adapter
::JSON
end
end
end
end
# frozen_string_literal: true
require "spec_helper"
RSpec.describe Gitlab::Json do
describe ".parse" do
it "parses an object" do
expect(subject.parse('{ "foo": "bar" }')).to eq({ "foo" => "bar" })
end
it "parses an array" do
expect(subject.parse('[{ "foo": "bar" }]')).to eq([{ "foo" => "bar" }])
end
it "raises an error on a string" do
expect { subject.parse('"foo"') }.to raise_error(JSON::ParserError)
end
it "raises an error on a true bool" do
expect { subject.parse("true") }.to raise_error(JSON::ParserError)
end
it "raises an error on a false bool" do
expect { subject.parse("false") }.to raise_error(JSON::ParserError)
end
end
describe ".parse!" do
it "parses an object" do
expect(subject.parse!('{ "foo": "bar" }')).to eq({ "foo" => "bar" })
end
it "parses an array" do
expect(subject.parse!('[{ "foo": "bar" }]')).to eq([{ "foo" => "bar" }])
end
it "raises an error on a string" do
expect { subject.parse!('"foo"') }.to raise_error(JSON::ParserError)
end
it "raises an error on a true bool" do
expect { subject.parse!("true") }.to raise_error(JSON::ParserError)
end
it "raises an error on a false bool" do
expect { subject.parse!("false") }.to raise_error(JSON::ParserError)
end
end
describe ".dump" do
it "dumps an object" do
expect(subject.dump({ "foo" => "bar" })).to eq('{"foo":"bar"}')
end
it "dumps an array" do
expect(subject.dump([{ "foo" => "bar" }])).to eq('[{"foo":"bar"}]')
end
it "dumps a string" do
expect(subject.dump("foo")).to eq('"foo"')
end
it "dumps a true bool" do
expect(subject.dump(true)).to eq("true")
end
it "dumps a false bool" do
expect(subject.dump(false)).to eq("false")
end
end
describe ".generate" do
it "delegates to the adapter" do
args = [{ foo: "bar" }]
expect(JSON).to receive(:generate).with(*args)
subject.generate(*args)
end
end
describe ".pretty_generate" do
it "delegates to the adapter" do
args = [{ foo: "bar" }]
expect(JSON).to receive(:pretty_generate).with(*args)
subject.pretty_generate(*args)
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment