Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
Codecrew
/
Moya
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
30
Merge Requests
2
Wiki
Snippets
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 {
...
@@ -70,12 +70,13 @@ public class JsonUtils {
* The value associate to the key
* The value associate to the key
* @return JsonObject with the key:value pair added
* @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
();
JsonObjectBuilder
builder
=
Json
.
createObjectBuilder
();
// Copy all non conflicting json entries
// Copy all non conflicting json entries
for
(
Map
.
Entry
<
String
,
JsonValue
>
v
:
jsonObject
.
entrySet
())
{
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
());
builder
=
builder
.
add
(
v
.
getKey
(),
v
.
getValue
());
}
}
}
}
...
@@ -102,7 +103,7 @@ public class JsonUtils {
...
@@ -102,7 +103,7 @@ public class JsonUtils {
// Recurse?
// Recurse?
if
(
keys
.
size
()
>
1
)
{
if
(
keys
.
size
()
>
1
)
{
String
firstKey
=
keys
.
get
(
0
);
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
);
JsonObject
subObj
=
jsonObject
.
getJsonObject
(
firstKey
);
return
assocJsonObject
(
jsonObject
,
firstKey
,
return
assocJsonObject
(
jsonObject
,
firstKey
,
assocInJsonObject
(
subObj
,
restKeys
,
value
));
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 {
...
@@ -64,7 +64,21 @@ public class JsonUtilsTest {
JsonObject
actual
=
JsonUtils
.
alterSubObject
(
meta
,
path
,
newData
);
JsonObject
actual
=
JsonUtils
.
alterSubObject
(
meta
,
path
,
newData
);
JsonObject
expected
=
jsonObject
(
"{\"foo\":\"bar\",\"baz\":{\"quux\":\"plop\"}}"
);
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