문제

Java에서 휠을 재발 명하고 내 자신의 JSON 구문 분석 방법을 작성합니다.

json.org 에 대한 (매우 멋진!) 문서로 가고 있습니다. 내가 확실하지 않은 유일한 부분은 "또는 제어 문자" 라고하는 곳입니다.

문서가 너무 명확하고 JSON이 너무 간단하고 구현하기 쉽고, 나는 앞으로 나아갈 것이라고 생각했고, 느슨한 대신 사양이 필요하다고 생각했습니다.

Java에서 제어 문자를 올바르게 어떻게 제거 할 수 있습니까? 아마도 유니 코드 범위가 있습니까?

여기에 이미지 설명


편집 : a (일반적으로?) 퍼즐에 누락 된 pirice

i 정의 된 범위 1 2 <script> 태그에서 귀찮은 일 수 있습니다.

가장 현저하게 특히 뉴라인 역할을하는 문자 U + 2028 및 U + 2029, LINE 및 단락 분리기. 문자열 리터럴 중간에 개행을 주입하면 구문 오류가 발생할 가능성이 큽니다 (unterminated String Literal). 3

이 문제가 XSS 위협을 제기하지 않는다고 믿지만 <script> 태그에서 사용하기위한 추가 규칙을 추가하는 것이 좋습니다.

  • 는 단순 해지고 \u 표기법으로 모든 비 "ASCII 인쇄용 문자를 인코딩합니다. 그 캐릭터는 흔하지 않습니다. 원한다면, 흰색 목록에 추가 할 수 있지만 흰색 목록 접근 방식을 권장합니다.
  • craters </script 문자가있는 페이지에 HTML 스크립트 인젝션 을 유발할 수있는 을 잊지 마십시오. 해당 문자 중 하나는 기본적으로 JSON에서 인코딩되지 않습니다.

도움이 되었습니까?

해결책

Will Character.isISOControl(...) do? Incidentally, UTF-16 is an encoding of Unicode codepoints... Are you going to be operating at the byte level, or at the character/codepoint level? I recommend leaving the mapping from UTF-16 to character streams to Java's core APIs...

다른 팁

Even if it's not very specific, I would assume that they refer to the "control" character category from the Unicode specification.

In Java, you can check if a character c is a Unicode control character with the following expression: Character.getType(c) == Character.CONTROL.

I believe the Unicode definition of a control character is:

The 65 characters in the ranges U+0000..U+001F and U+007F..U+009F.

That's their definition of a control code, but the above is followed by the sentence "Also known as control characters.", so...

I know the question has been asked a couple of years ago, but I am replying anyway, because the accepted answer is not correct.

Character.isISOControl(int codePoint) 

does the following check:

(codePoint >= 0x00 && codePoint <= 0x1F) || (codePoint >= 0x7F && codePoint <= 0x9F);

The JSON specification defines at https://tools.ietf.org/html/rfc7159:

  1. Strings

    The representation of strings is similar to conventions used in the C family of programming languages. A string begins and ends with quotation marks. All Unicode characters may be placed within the quotation marks, except for the characters that must be escaped: quotation mark, reverse solidus, and the control characters (U+0000 through U+001F).

Character.isISOControl(int codePoint) 

will flag all characters that need to be escaped (U+0000-U+001F), though it will also flag characters that do not need to be escaped (U+007F-U+009F). It is not required to escape the characters (U+007F-U+009F).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top