Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 140 additions & 10 deletions library/kernel/json_array.e
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,20 @@ inherit

ITERABLE [JSON_VALUE]

JSON_COLLECTION
redefine
items
end

DEBUG_OUTPUT

create
make, make_empty,
make,
make_empty,
make_from_separate,
make_array


feature {NONE} -- Initialization

make (nb: INTEGER)
Expand All @@ -51,22 +59,46 @@ feature {NONE} -- Initialization
make (10)
end

make_from_separate (other: separate like Current)
-- <Precursor>
do
create items.make (other.capacity)
append_from_separate (other)
ensure then
capacity = other.capacity
count = other.count
end

feature -- Status report

is_array: BOOLEAN = True
-- <Precursor>

feature -- Access

i_th alias "[]" (i: INTEGER): JSON_VALUE
first: like items.item
require
not_empty: not is_empty
do
Result := items.i_th (1)
end

last: like items.item
require
not_empty: not is_empty
do
Result := items.i_th (count)
end

i_th alias "[]" (i: INTEGER): like items.item
-- Item at `i'-th position
require
is_valid_index: valid_index (i)
do
Result := items.i_th (i)
end

chained_item alias "@" (a_key: JSON_STRING): JSON_VALUE
chained_item alias "@" (a_key: JSON_STRING): like items.item
-- <Precursor>.
do
if a_key.item.is_integer then
Expand All @@ -88,6 +120,9 @@ feature -- Access
Result.append (ic.item.representation)
end
Result.append_character (']')
ensure then
Result.starts_with ("[")
Result.ends_with ("]")
end

feature -- Visitor pattern
Expand All @@ -101,20 +136,26 @@ feature -- Visitor pattern

feature -- Access

new_cursor: ITERATION_CURSOR [JSON_VALUE]
new_cursor: like items.new_cursor
-- Fresh cursor associated with current structure
do
Result := items.new_cursor
end

feature -- Mesurement

count: INTEGER
count: like items.count
-- Number of items.
do
Result := items.count
end

capacity: like items.capacity
-- Number of items that may be stored.
do
Result := items.capacity
end

feature -- Status report

is_empty: BOOLEAN
Expand All @@ -131,7 +172,7 @@ feature -- Status report

feature -- Change Element

put_front (v: JSON_VALUE)
put_front (v: like items.item)
require
v_not_void: v /= Void
do
Expand All @@ -140,7 +181,7 @@ feature -- Change Element
has_new_value: old items.count + 1 = items.count and items.first = v
end

add, extend (v: JSON_VALUE)
add, extend (v: like items.item)
require
v_not_void: v /= Void
do
Expand All @@ -149,7 +190,7 @@ feature -- Change Element
has_new_value: old items.count + 1 = items.count and items.has (v)
end

prune_all (v: JSON_VALUE)
prune_all (v: like items.item)
-- Remove all occurrences of `v'.
require
v_not_void: v /= Void
Expand All @@ -159,10 +200,25 @@ feature -- Change Element
not_has_new_value: not items.has (v)
end

append_from_separate (other: separate like Current)
-- Appends Current with content of other `other'
local
l_item_non_sep: JSON_VALUE
do
across
other as l_item
loop
l_item_non_sep := non_sep_json_value (l_item.item)
extend (l_item_non_sep)
end
end

wipe_out
-- Remove all items.
do
items.wipe_out
ensure
is_empty
end

feature -- Report
Expand All @@ -187,13 +243,87 @@ feature -- Report

feature -- Conversion

array_representation: ARRAYED_LIST [JSON_VALUE]
array_representation: like items
-- Representation as a sequences of values.
-- be careful, modifying the return object may have impact on the original JSON_ARRAY object.
do
Result := items
end

non_sep_json_value (a_json_v: separate JSON_VALUE): JSON_VALUE
-- dirty but don't see any other option, see https://github.com/eiffelhub/json/issues/19#issuecomment-592171283 issue for details
local
l_json_s: JSON_STRING
l_s: STRING
do
-- - JSON_ARRAY: no assertion
-- - JSON_BOOLEAN: no assertion
-- - JSON_NULL: no assertion
-- - JSON_NUMBER: no assertion
-- - JSON_OBJECT: no assertion
-- - JSON_STRING: no assertion
if attached {separate JSON_ARRAY} a_json_v as l_json then
create {JSON_ARRAY} Result.make (l_json.count)
check
attached {JSON_ARRAY} Result as l_res
then
across
l_json as l_item
loop
check
attached {JSON_VALUE} non_sep_json_value (l_item.item) as l_non_sep_json_value
then
l_res.extend (l_non_sep_json_value)
end
end
end
elseif attached {separate JSON_BOOLEAN} a_json_v as l_json then
create {JSON_BOOLEAN} Result.make (l_json.item)
elseif attached {separate JSON_STRING} a_json_v as l_json then
create l_s.make_from_separate (l_json.item)
create {JSON_STRING} Result.make_from_string (l_s)
elseif attached {separate JSON_NULL} a_json_v as l_json then
create {JSON_NULL} Result
elseif attached {separate JSON_NUMBER} a_json_v as l_json then
if l_json.is_integer then
create {JSON_NUMBER} Result.make_integer (l_json.integer_64_item)
elseif l_json.is_real then
create {JSON_NUMBER} Result.make_real (l_json.real_64_item)
elseif l_json.is_natural then
create {JSON_NUMBER} Result.make_natural (l_json.natural_64_item)
else
create {JSON_NUMBER} Result.make_natural (0)
check
invalid_json_number: False
end
end
elseif attached {separate JSON_OBJECT} a_json_v as l_json then
create {JSON_OBJECT} Result.make_with_capacity (l_json.count)
check
attached {JSON_OBJECT} Result as l_res
then
across
l_json as l_item
loop
check
attached {JSON_VALUE} non_sep_json_value (l_item.item) as l_non_sep_json_value
then
create l_s.make_from_separate (l_item.key.item)
create l_json_s.make_from_string (l_s)
l_res.put (l_non_sep_json_value, l_json_s)
end
end
end
else
create {JSON_ARRAY} Result.make_empty
check
not_implemented: False
end
end
ensure
instance_free: Class
end

feature -- Status report

debug_output: STRING
Expand All @@ -211,6 +341,6 @@ invariant
items_not_void: items /= Void

note
copyright: "2010-2018, Javier Velilla, Jocelyn Fiat, Eiffel Software and others https://github.com/eiffelhub/json."
copyright: "2010-2020, Javier Velilla, Jocelyn Fiat, Eiffel Software and others https://github.com/eiffelhub/json."
license: "https://github.com/eiffelhub/json/blob/master/License.txt"
end
12 changes: 10 additions & 2 deletions library/kernel/json_boolean.e
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ inherit

create
make,
make_true, make_false,
make_true,
make_false,
make_from_separate,
make_boolean

feature {NONE} -- Initialization
Expand Down Expand Up @@ -44,6 +46,12 @@ feature {NONE} -- Initialization
make (a_item)
end

make_from_separate (other: separate like Current)
-- <Precursor>
do
make (other.item)
end

feature -- Access

item: BOOLEAN
Expand Down Expand Up @@ -82,6 +90,6 @@ feature -- Status report
end

note
copyright: "2010-2017, Javier Velilla and others https://github.com/eiffelhub/json."
copyright: "2010-2020, Javier Velilla, Jocelyn Fiat, Eiffel Software and others https://github.com/eiffelhub/json."
license: "https://github.com/eiffelhub/json/blob/master/License.txt"
end
19 changes: 19 additions & 0 deletions library/kernel/json_collection.e
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
note
description: "JSON Collection of items"
author: ""
date: "$Date$"
revision: "$Revision$"

deferred class
JSON_COLLECTION


feature {NONE} -- Implementation

items: READABLE_INDEXABLE [JSON_VALUE]
-- Value container

;note
copyright: "2010-2020, Javier Velilla, Jocelyn Fiat, Eiffel Software and others https://github.com/eiffelhub/json."
license: "https://github.com/eiffelhub/json/blob/master/License.txt"
end
13 changes: 12 additions & 1 deletion library/kernel/json_null.e
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ inherit
is_null
end

create
default_create,
make_from_separate

feature {NONE} -- Initialization

make_from_separate (other: separate like Current)
-- <Precursor>
do
end

feature -- Status report

is_null: BOOLEAN = True
Expand Down Expand Up @@ -57,6 +68,6 @@ feature {NONE} -- Implementation
null_value: STRING = "null"

note
copyright: "2010-2019, Javier Velilla, Jocelyn Fiat, Eiffel Software and others https://github.com/eiffelhub/json."
copyright: "2010-2020, Javier Velilla, Jocelyn Fiat, Eiffel Software and others https://github.com/eiffelhub/json."
license: "https://github.com/eiffelhub/json/blob/master/License.txt"
end
16 changes: 14 additions & 2 deletions library/kernel/json_number.e
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ inherit
end

create
make_integer, make_natural, make_real
make_integer,
make_natural,
make_from_separate,
make_real

feature {NONE} -- initialization

Expand Down Expand Up @@ -49,6 +52,15 @@ feature {NONE} -- initialization
numeric_type := double_type
end

make_from_separate (other: separate like Current)
-- <Precursor>
do
item := create {STRING}.make_from_separate (other.item.out) -- Not quite sure of that
numeric_type := other.numeric_type
ensure then
numeric_type = other.numeric_type
end

feature -- Status report

is_number: BOOLEAN = True
Expand Down Expand Up @@ -199,6 +211,6 @@ invariant
inf_only_for_real: inv_item.is_case_insensitive_equal_general (positive_infinity_real_value) implies is_real

note
copyright: "2010-2019, Javier Velilla, Jocelyn Fiat, Eiffel Software and others https://github.com/eiffelhub/json."
copyright: "2010-2020, Javier Velilla, Jocelyn Fiat, Eiffel Software and others https://github.com/eiffelhub/json."
license: "https://github.com/eiffelhub/json/blob/master/License.txt"
end
Loading