首页 > 编程语言 >[java] JSON格式校验

[java] JSON格式校验

时间:2023-02-07 10:02:08浏览次数:52  
标签:return 校验 private nextCharacter JSON boolean error java col


对JSON字符串进行格式校验,不依赖于第三方包

package com.iaiai.test;

import java.text.CharacterIterator;
import java.text.StringCharacterIterator;

/**
*
* <br/>
* Title: JsonValidator.java<br/>
* E-Mail: [email protected]<br/>
* QQ: 176291935<br/>
* Http: iaiai.iteye.com<br/>
* Create time: 2013-1-29 上午11:29:09<br/>
* <br/>
*
* @author 丸子
* @version 0.0.1
*/
public class JsonValidator {

private CharacterIterator it;
private char c;
private int col;

public JsonValidator() {
}

/**
* 验证一个字符串是否是合法的JSON串
*
* @param input
* 要验证的字符串
* @return true-合法 ,false-非法
*/
public boolean validate(String input) {
input = input.trim();
boolean ret = valid(input);
return ret;
}

private boolean valid(String input) {
if ("".equals(input))
return true;

boolean ret = true;
it = new StringCharacterIterator(input);
c = it.first();
col = 1;
if (!value()) {
ret = error("value", 1);
} else {
skipWhiteSpace();
if (c != CharacterIterator.DONE) {
ret = error("end", col);
}
}

return ret;
}

private boolean value() {
return literal("true") || literal("false") || literal("null") || string() || number() || object() || array();
}

private boolean literal(String text) {
CharacterIterator ci = new StringCharacterIterator(text);
char t = ci.first();
if (c != t)
return false;

int start = col;
boolean ret = true;
for (t = ci.next(); t != CharacterIterator.DONE; t = ci.next()) {
if (t != nextCharacter()) {
ret = false;
break;
}
}
nextCharacter();
if (!ret)
error("literal " + text, start);
return ret;
}

private boolean array() {
return aggregate('[', ']', false);
}

private boolean object() {
return aggregate('{', '}', true);
}

private boolean aggregate(char entryCharacter, char exitCharacter, boolean prefix) {
if (c != entryCharacter)
return false;
nextCharacter();
skipWhiteSpace();
if (c == exitCharacter) {
nextCharacter();
return true;
}

for (;;) {
if (prefix) {
int start = col;
if (!string())
return error("string", start);
skipWhiteSpace();
if (c != ':')
return error("colon", col);
nextCharacter();
skipWhiteSpace();
}
if (value()) {
skipWhiteSpace();
if (c == ',') {
nextCharacter();
} else if (c == exitCharacter) {
break;
} else {
return error("comma or " + exitCharacter, col);
}
} else {
return error("value", col);
}
skipWhiteSpace();
}

nextCharacter();
return true;
}

private boolean number() {
if (!Character.isDigit(c) && c != '-')
return false;
int start = col;
if (c == '-')
nextCharacter();
if (c == '0') {
nextCharacter();
} else if (Character.isDigit(c)) {
while (Character.isDigit(c))
nextCharacter();
} else {
return error("number", start);
}
if (c == '.') {
nextCharacter();
if (Character.isDigit(c)) {
while (Character.isDigit(c))
nextCharacter();
} else {
return error("number", start);
}
}
if (c == 'e' || c == 'E') {
nextCharacter();
if (c == '+' || c == '-') {
nextCharacter();
}
if (Character.isDigit(c)) {
while (Character.isDigit(c))
nextCharacter();
} else {
return error("number", start);
}
}
return true;
}

private boolean string() {
if (c != '"')
return false;

int start = col;
boolean escaped = false;
for (nextCharacter(); c != CharacterIterator.DONE; nextCharacter()) {
if (!escaped && c == '\\') {
escaped = true;
} else if (escaped) {
if (!escape()) {
return false;
}
escaped = false;
} else if (c == '"') {
nextCharacter();
return true;
}
}
return error("quoted string", start);
}

private boolean escape() {
int start = col - 1;
if (" \\\"/bfnrtu".indexOf(c) < 0) {
return error("escape sequence \\\",\\\\,\\/,\\b,\\f,\\n,\\r,\\t or \\uxxxx ", start);
}
if (c == 'u') {
if (!ishex(nextCharacter()) || !ishex(nextCharacter()) || !ishex(nextCharacter())
|| !ishex(nextCharacter())) {
return error("unicode escape sequence \\uxxxx ", start);
}
}
return true;
}

private boolean ishex(char d) {
return "0123456789abcdefABCDEF".indexOf(c) >= 0;
}

private char nextCharacter() {
c = it.next();
++col;
return c;
}

private void skipWhiteSpace() {
while (Character.isWhitespace(c)) {
nextCharacter();
}
}

private boolean error(String type, int col) {
System.out.printf("type: %s, col: %s%s", type, col, System.getProperty("line.separator"));
return false;
}

public static void main(String[] args) {
String jsonStr = "{\"website\":\"1.11\"}";
System.out.println(jsonStr + ":" + new JsonValidator().validate(jsonStr));
}
}

标签:return,校验,private,nextCharacter,JSON,boolean,error,java,col
From: https://blog.51cto.com/u_15955464/6041287

相关文章

  • [java] 判断两台机器之间网络是否可达
    Java类库判断两台机器之间网络是否可达,常用ping方法来实现。packagecom.iaiai.test;importjava.net.InetAddress;/****<br/>*Title:Test.ja......
  • 用java实现zip压缩
    本来是写到spaceslive上的,可是代码的显示效果确实不怎么好看。在javaeye上试了试代码显示的顺眼多了。今天写了个用java压缩的功能,可以实现对文件......
  • java.lang.SecurityException: Access to default session denied
    {exception=java.lang.SecurityException:Accesstodefaultsessiondenied}java.lang.SecurityException:Accesstodefaultsessiondeniedat......
  • 根据经纬度求两点间距离实现源码(java)
    研究了一下GoogleMap上的根据经纬度求地球表面两点间距离的实现,用java实现了一把,对我国境内的Beijing54,Xian80,WGS84三种坐标系的空间距离计算感......
  • ImageIO读jpg的时候出现javax.imageio.IIOException: Unsupported Image Type
    用java处理图片的时候,ImageIO读jpg的时候出现exception:bandOffsets.lengthiswrong。我也遇到过,最终也解决啦,把成果跟大家分享。原因可能是JPEG图......
  • java 最短路径算法
    packageorg.iaiai.suanfa;importjava.util.ArrayList;/****<p>*Title:Dijkstra.java*</p>*<p>*Http:iaiai.iteye.com*</p>*<p>*Createtime:2011-8-......
  • java 广度优先
    就是按照正常顺序把tree打出来,假设有个tree是1/\23/\/\4567打印出来就是1,2,3,4,5,6,7;这个就是宽度优先packageorg.iaiai.suanfa;i......
  • java 深度优先搜索(回溯法)
    深度优先遍历类似于树的前序遍历。采用的搜索方法的特点是尽可能先对纵深方向进行搜索。这种搜索方法称为深度优先搜索(Depth-FirstSearch)。相应地,......
  • Java+Swing实现学生选课管理系统 (1)
    @目录一、系统介绍二、系统展示1.课程查询2.课程添加3.退课三、系统实现四、获取源码一、系统介绍本系统实现了学生登录和管理员登录,学生实现选课,查看已选课程,修改密码,查......
  • Java+Swing+dat文件存储实现学生选课管理系统
    @目录一、系统介绍二、系统展示1.用户登陆、注册2.课程信息查询3.添加课程4.选课5.退课三、系统实现四、.获取源码一、系统介绍功能展示:用户注册、用户登陆课程管理:课程......