Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dolibarr
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container registry
Model registry
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Software_Artifact_Infrastructure_Repository
dolibarr
Commits
de97cd18
Commit
de97cd18
authored
11 years ago
by
Laurent Destailleur
Browse files
Options
Downloads
Patches
Plain Diff
Fix: json
parent
56730741
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
htdocs/core/lib/json.lib.php
+6
-4
6 additions, 4 deletions
htdocs/core/lib/json.lib.php
test/phpunit/JsonLibTest.php
+22
-9
22 additions, 9 deletions
test/phpunit/JsonLibTest.php
with
28 additions
and
13 deletions
htdocs/core/lib/json.lib.php
+
6
−
4
View file @
de97cd18
...
...
@@ -54,14 +54,16 @@ function dol_json_encode($elements)
//var_dump($num);
// determine type
if
(
is_numeric
(
key
(
$elements
)))
if
(
is_numeric
(
key
(
$elements
))
&&
key
(
$elements
)
==
0
)
{
// indexed (list)
$keysofelements
=
array_keys
(
$elements
);
// Elements array mus have key that does not start with 0 and end with num-1, so we will use this later.
$output
=
'['
;
for
(
$i
=
0
,
$last
=
(
$num
-
1
);
isset
(
$elements
[
$i
]);
++
$i
)
for
(
$i
=
0
,
$last
=
(
$num
-
1
);
$i
<
$num
;
$i
++
)
{
if
(
is_array
(
$elements
[
$i
])
||
is_object
(
$elements
[
$i
]))
$output
.
=
json_encode
(
$elements
[
$i
]);
else
$output
.
=
_val
(
$elements
[
$i
]);
if
(
!
isset
(
$elements
[
$keysofelements
[
$i
]]))
continue
;
if
(
is_array
(
$elements
[
$keysofelements
[
$i
]])
||
is_object
(
$elements
[
$keysofelements
[
$i
]]))
$output
.
=
json_encode
(
$elements
[
$keysofelements
[
$i
]]);
else
$output
.
=
_val
(
$elements
[
$keysofelements
[
$i
]]);
if
(
$i
!==
$last
)
$output
.
=
','
;
}
$output
.
=
']'
;
...
...
This diff is collapsed.
Click to expand it.
test/phpunit/JsonLibTest.php
+
22
−
9
View file @
de97cd18
...
...
@@ -130,22 +130,35 @@ class JsonLibTest extends PHPUnit_Framework_TestCase
$this
->
savlangs
=
$langs
;
$this
->
savdb
=
$db
;
// Do a test with an array starting with 0
$arraytotest
=
array
(
0
=>
array
(
'key'
=>
1
,
'value'
=>
'PRODREF'
,
'label'
=>
'Product ref with é and special chars \\ \' "'
));
$arrayencodedexpected
=
'[{"key":1,"value":"PRODREF","label":"Product ref with \u00e9 and special chars \\\\ \' \""}]'
;
$encoded
=
json_encode
(
$arraytotest
);
//var_dump($encoded);
$this
->
assertEquals
(
'[{"key":1,"value":"PRODREF","label":"Product ref with \u00e9 and special chars \\\\ \' \""}]'
,
$encoded
);
$this
->
assertEquals
(
$arrayencodedexpected
,
$encoded
);
$decoded
=
json_decode
(
$encoded
,
true
);
//var_dump($decoded);
$this
->
assertEquals
(
$arraytotest
,
$decoded
);
$this
->
assertEquals
(
$arraytotest
,
$decoded
,
'test for json_xxx'
);
$encoded
=
dol_json_encode
(
$arraytotest
);
//var_dump($encoded);
$this
->
assertEquals
(
'[{"key":1,"value":"PRODREF","label":"Product ref with \u00e9 and special chars \\\\ \' \""}]'
,
$encoded
);
$this
->
assertEquals
(
$arrayencodedexpected
,
$encoded
);
$decoded
=
dol_json_decode
(
$encoded
,
true
);
//var_dump($decoded);
$this
->
assertEquals
(
$arraytotest
,
$decoded
);
$this
->
assertEquals
(
$arraytotest
,
$decoded
,
'test for dol_json_xxx'
);
// Same test but array start with 2 instead of 0
$arraytotest
=
array
(
2
=>
array
(
'key'
=>
1
,
'value'
=>
'PRODREF'
,
'label'
=>
'Product ref with é and special chars \\ \' "'
));
$arrayencodedexpected
=
'{"2":{"key":1,"value":"PRODREF","label":"Product ref with \u00e9 and special chars \\\\ \' \""}}'
;
$encoded
=
json_encode
(
$arraytotest
);
$this
->
assertEquals
(
$arrayencodedexpected
,
$encoded
);
$decoded
=
json_decode
(
$encoded
,
true
);
$this
->
assertEquals
(
$arraytotest
,
$decoded
,
'test for json_xxx'
);
$encoded
=
dol_json_encode
(
$arraytotest
);
$this
->
assertEquals
(
$arrayencodedexpected
,
$encoded
);
$decoded
=
dol_json_decode
(
$encoded
,
true
);
$this
->
assertEquals
(
$arraytotest
,
$decoded
,
'test for dol_json_xxx'
);
// Test with object
$now
=
gmmktime
(
12
,
0
,
0
,
1
,
1
,
1970
);
$objecttotest
=
new
stdClass
();
$objecttotest
->
property1
=
'abc'
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment