Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
L
LanguageFileCompare
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Jira
Jira
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
測試專案
LanguageFileCompare
Commits
63fb38d2
Commit
63fb38d2
authored
Nov 26, 2018
by
王品堯
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
check json files
parent
c183e3b8
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
152 additions
and
0 deletions
+152
-0
.gitignore
.gitignore
+3
-0
README.md
README.md
+13
-0
pom.xml
pom.xml
+47
-0
src/main/java/JsonFileCompare.java
src/main/java/JsonFileCompare.java
+89
-0
No files found.
.gitignore
0 → 100644
View file @
63fb38d2
# Created by .ignore support plugin (hsz.mobi)
JsonFileCompare.iml
target/
README.md
View file @
63fb38d2
# JsonFileCompare
### Find missing key in Target json file
```
mvn package
```
可以在target下找到JsonFileCompare-exec.jar
```
java
java
-
cp
JsonFileCompare
-
exec
.
jar
JsonFileCompare
/
xxx
/
locale
-
zh_TW
.
json
/
xxx
/
locale
-
zh_CN
.
json
/
xxx
/
locale
-
en_US
.
json
```
P.S 第一個參數為基準的json, 後面為要找出缺少key的目標檔案
pom.xml
0 → 100644
View file @
63fb38d2
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
<groupId>
org.ylhealth
</groupId>
<artifactId>
JsonFileCompare
</artifactId>
<version>
0.0.1
</version>
<packaging>
jar
</packaging>
<properties>
<maven.compiler.source>
1.8
</maven.compiler.source>
<maven.compiler.target>
1.8
</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>
com.fasterxml.jackson.core
</groupId>
<artifactId>
jackson-databind
</artifactId>
<version>
2.9.7
</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- any other plugins -->
<plugin>
<artifactId>
maven-assembly-plugin
</artifactId>
<executions>
<execution>
<phase>
package
</phase>
<goals>
<goal>
single
</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>
jar-with-dependencies
</descriptorRef>
</descriptorRefs>
<finalName>
JsonFileCompare-exec
</finalName>
<appendAssemblyId>
false
</appendAssemblyId>
</configuration>
</plugin>
</plugins>
</build>
</project>
src/main/java/JsonFileCompare.java
0 → 100644
View file @
63fb38d2
import
com.fasterxml.jackson.databind.JsonNode
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.fasterxml.jackson.databind.node.ArrayNode
;
import
com.fasterxml.jackson.databind.node.ObjectNode
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
import
java.util.*
;
import
java.util.stream.Collectors
;
import
java.util.stream.IntStream
;
public
class
JsonFileCompare
{
public
static
void
main
(
String
[]
args
)
{
try
{
ObjectMapper
objectMapper
=
new
ObjectMapper
();
Path
originPath
=
Paths
.
get
(
args
[
0
]);
JsonNode
twJson
=
objectMapper
.
readTree
(
new
String
(
Files
.
readAllBytes
(
originPath
)));
Set
<
String
>
originSet
=
new
HashSet
<>();
addKeys
(
""
,
twJson
,
originSet
,
new
ArrayList
<>());
IntStream
.
range
(
1
,
args
.
length
).
forEach
(
i
->
findMissingKeys
(
Paths
.
get
(
args
[
i
]),
originSet
));
}
catch
(
Exception
e
)
{
System
.
err
.
println
(
args
[
0
]
+
" is not exists or not json files."
);
System
.
exit
(
1
);
}
}
public
static
void
addKeys
(
String
currentPath
,
JsonNode
jsonNode
,
Set
set
,
List
<
Integer
>
suffix
)
{
if
(
jsonNode
.
isObject
())
{
ObjectNode
objectNode
=
(
ObjectNode
)
jsonNode
;
Iterator
<
Map
.
Entry
<
String
,
JsonNode
>>
iter
=
objectNode
.
fields
();
String
pathPrefix
=
currentPath
.
isEmpty
()
?
""
:
currentPath
+
"-"
;
while
(
iter
.
hasNext
())
{
Map
.
Entry
<
String
,
JsonNode
>
entry
=
iter
.
next
();
addKeys
(
pathPrefix
+
entry
.
getKey
(),
entry
.
getValue
(),
set
,
suffix
);
}
}
else
if
(
jsonNode
.
isArray
())
{
ArrayNode
arrayNode
=
(
ArrayNode
)
jsonNode
;
for
(
int
i
=
0
;
i
<
arrayNode
.
size
();
i
++)
{
suffix
.
add
(
i
+
1
);
addKeys
(
currentPath
,
arrayNode
.
get
(
i
),
set
,
suffix
);
if
(
i
+
1
<
arrayNode
.
size
())
{
suffix
.
remove
(
arrayNode
.
size
()
-
1
);
}
}
}
else
if
(
jsonNode
.
isValueNode
())
{
if
(
currentPath
.
contains
(
"-"
))
{
for
(
int
i
=
0
;
i
<
suffix
.
size
();
i
++)
{
currentPath
+=
"-"
+
suffix
.
get
(
i
);
}
}
set
.
add
(
currentPath
);
}
}
public
static
void
findMissingKeys
(
Path
targetPath
,
Set
<
String
>
originSet
)
{
if
(!
targetPath
.
toFile
().
exists
())
return
;
try
{
Set
<
String
>
targetSet
=
new
HashSet
<>();
ObjectMapper
objectMapper
=
new
ObjectMapper
();
JsonNode
json
=
objectMapper
.
readTree
(
new
String
(
Files
.
readAllBytes
(
targetPath
)));
addKeys
(
""
,
json
,
targetSet
,
new
ArrayList
<>());
Set
<
String
>
missingKey
=
originSet
.
parallelStream
()
.
filter
(
key
->
!
targetSet
.
contains
(
key
))
.
collect
(
Collectors
.
toSet
());
if
(!
missingKey
.
isEmpty
())
{
System
.
err
.
println
(
"Missing Key on "
+
targetPath
.
toString
());
System
.
err
.
println
(
"Missing Keys: "
);
missingKey
.
stream
().
sorted
().
forEach
(
System
.
err
::
println
);
System
.
exit
(
1
);
}
}
catch
(
Exception
e
)
{
System
.
err
.
println
(
targetPath
.
toString
()
+
" is not json files."
);
System
.
exit
(
1
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
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 comment