2024-01-16 21:26:16 +08:00
/ *
Language : JSON
Description : JSON ( JavaScript Object Notation ) is a lightweight data - interchange format .
Author : Ivan Sagalaev < maniac @ softwaremaniacs . org >
Website : http : //www.json.org
Category : common , protocols
* /
function json ( hljs ) {
const LITERALS = {
literal : 'true false null'
} ;
const ALLOWED _COMMENTS = [
hljs . C _LINE _COMMENT _MODE ,
hljs . C _BLOCK _COMMENT _MODE
] ;
const TYPES = [
2023-12-18 13:12:25 +08:00
hljs . QUOTE _STRING _MODE ,
hljs . C _NUMBER _MODE
] ;
2024-01-16 21:26:16 +08:00
const VALUE _CONTAINER = {
end : ',' ,
endsWithParent : true ,
excludeEnd : true ,
2023-12-18 13:12:25 +08:00
contains : TYPES ,
keywords : LITERALS
} ;
2024-01-16 21:26:16 +08:00
const OBJECT = {
begin : /\{/ ,
end : /\}/ ,
2023-12-18 13:12:25 +08:00
contains : [
{
className : 'attr' ,
2024-01-16 21:26:16 +08:00
begin : /"/ ,
end : /"/ ,
2023-12-18 13:12:25 +08:00
contains : [ hljs . BACKSLASH _ESCAPE ] ,
2024-01-16 21:26:16 +08:00
illegal : '\\n'
2023-12-18 13:12:25 +08:00
} ,
2024-01-16 21:26:16 +08:00
hljs . inherit ( VALUE _CONTAINER , {
begin : /:/
} )
] . concat ( ALLOWED _COMMENTS ) ,
2023-12-18 13:12:25 +08:00
illegal : '\\S'
} ;
2024-01-16 21:26:16 +08:00
const ARRAY = {
begin : '\\[' ,
end : '\\]' ,
2023-12-18 13:12:25 +08:00
contains : [ hljs . inherit ( VALUE _CONTAINER ) ] , // inherit is a workaround for a bug that makes shared modes with endsWithParent compile only the ending of one of the parents
illegal : '\\S'
} ;
2024-01-16 21:26:16 +08:00
TYPES . push ( OBJECT , ARRAY ) ;
ALLOWED _COMMENTS . forEach ( function ( rule ) {
TYPES . push ( rule ) ;
} ) ;
2023-12-18 13:12:25 +08:00
return {
2024-01-16 21:26:16 +08:00
name : 'JSON' ,
2023-12-18 13:12:25 +08:00
contains : TYPES ,
keywords : LITERALS ,
illegal : '\\S'
} ;
2024-01-16 21:26:16 +08:00
}
module . exports = json ;