Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
Max Mecklin
/
Moya
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 67bd2ad5
authored
Nov 07, 2014
by
Juho Juopperi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
also fix the code that failed the test before
1 parent
c3a40db9
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
19 additions
and
4 deletions
code/moya-utils/src/main/java/fi/codecrew/moya/utilities/JsonUtils.java
code/moya-utils/src/test/java/fi/codecrew/moya/utilities/JsonUtilsTest.java
code/moya-utils/src/main/java/fi/codecrew/moya/utilities/JsonUtils.java
View file @
67bd2ad
...
...
@@ -70,12 +70,13 @@ public class JsonUtils {
* The value associate to the key
* @return JsonObject with the key:value pair added
*/
public
static
JsonObject
assocJsonObject
(
JsonObject
jsonObject
,
String
key
,
JsonValue
value
)
{
public
static
JsonObject
assocJsonObject
(
JsonObject
jsonObject
,
String
key
,
JsonValue
value
)
{
JsonObjectBuilder
builder
=
Json
.
createObjectBuilder
();
// Copy all non conflicting json entries
for
(
Map
.
Entry
<
String
,
JsonValue
>
v
:
jsonObject
.
entrySet
())
{
if
(
v
.
getKey
().
equals
(
key
))
{
if
(
v
.
getKey
().
equals
(
key
)
==
false
)
{
builder
=
builder
.
add
(
v
.
getKey
(),
v
.
getValue
());
}
}
...
...
@@ -102,7 +103,7 @@ public class JsonUtils {
// Recurse?
if
(
keys
.
size
()
>
1
)
{
String
firstKey
=
keys
.
get
(
0
);
List
<
String
>
restKeys
=
keys
.
subList
(
0
,
keys
.
size
());
List
<
String
>
restKeys
=
keys
.
subList
(
1
,
keys
.
size
());
JsonObject
subObj
=
jsonObject
.
getJsonObject
(
firstKey
);
return
assocJsonObject
(
jsonObject
,
firstKey
,
assocInJsonObject
(
subObj
,
restKeys
,
value
));
...
...
code/moya-utils/src/test/java/fi/codecrew/moya/utilities/JsonUtilsTest.java
View file @
67bd2ad
...
...
@@ -64,7 +64,21 @@ public class JsonUtilsTest {
JsonObject
actual
=
JsonUtils
.
alterSubObject
(
meta
,
path
,
newData
);
JsonObject
expected
=
jsonObject
(
"{\"foo\":\"bar\",\"baz\":{\"quux\":\"plop\"}}"
);
Assert
.
assertEquals
(
expected
.
toString
(),
actual
.
toString
());
Assert
.
assertEquals
(
actual
.
toString
(),
expected
.
toString
());
}
@Test
public
final
void
testAlterSubSubObject
()
{
JsonObject
meta
=
jsonObject
(
"{\"bystander1\":\"canary1\", \"module1\":{\"bystander2\":\"canary2\",\"submodule1\":{\"replacee1\":\"replacedvalue1\",\"r2\":\"rv2\"}}}"
);
JsonObject
newData
=
jsonObject
(
"{\"quux\":\"plop\"}"
);
ArrayList
<
String
>
path
=
new
ArrayList
<
String
>();
path
.
add
(
"module1"
);
path
.
add
(
"submodule1"
);
JsonObject
actual
=
JsonUtils
.
alterSubObject
(
meta
,
path
,
newData
);
JsonObject
expected
=
jsonObject
(
"{\"bystander1\":\"canary1\", \"module1\":{\"bystander2\":\"canary2\",\"submodule1\":{\"quux\":\"plop\"}}}"
);
Assert
.
assertEquals
(
actual
.
toString
(),
expected
.
toString
());
}
}
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment