Enumerating JSON string literals

How many JSON string literals of length N are there?

As with our previous essay, the answers depends what you mean by "string" and "length".

Our calculation is also made slightly more complicated by the definition of a JSON string literal. A JSON string literal starts and ends with a single element, a double quote ". This means immediately that there are no valid JSON string literals of length 0 or 1. Furthermore, the string "interior", between the two double quotes, is a sequence of zero or more "characters", where a character can be expressed in three different ways:

  • Unicode code points U+0020 to U+10FFFF inclusive, except for the double quote " and the backslash \, may simply appear literally. There are 1,114,078 of these.

  • There are also 8 simple backslash escape sequences. Each is 2 code points long, consisting of a backslash \ followed by one of 8 possible code points: ", /, \, b, f, n, r or t. For example, \n. What these escape sequences actually mean/do is actually not important for this piece of work.

  • Lastly there are hex escapes. These are 6 code points long, consisting of a backslash \, a u and 4 hexadecimal digits. For example, \u0020. As the hexadecimal digits are not case-sensitive, there are not 16 but 22 possible hexadecimal digits, and not 164 = 65,536 but 224 = 234,256 possible hex escape sequences. These create distinct strings so we count them all, even if a consumer would almost certainly treat \uffff and \uFFFF identically.

Contents

A string is a sequence of Unicode code points

If a string is simply a sequence of Unicode code points with no other constraints, the number of JSON string literals of length N + 2 is:

a0 = 1 a1 = 1114078a0 a2 = 8a0 + 1114078a1 a3 = 8a1 + 1114078a2 a4 = 8a2 + 1114078a3 a5 = 8a3 + 1114078a4 aN = 234256aN-6 + 8aN-2 + 1114078aN-1 (N≥6)

Or...

( aN-5 aN-4 aN-3 aN-2 aN-1 aN ) = ( 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 234256 0 0 0 8 1114078 ) N ( 0 0 0 0 0 1 ) (N≥0)

Memoization makes this pretty fast to compute. The number of operations is O(N), though of course the time taken for each operation is not really constant.

A table of these values is as follows:

N Number of Unicode JSON string literals of length N + 2
01
11,114,078
21,241,169,790,092
31,382,759,957,415,027,800
41,540,502,447,846,948,699,689,136
51,716,239,886,103,494,993,111,593,479,008
61,912,025,099,842,733,514,518,553,429,504,021,968
72,130,145,099,196,322,787,476,628,927,580,135,513,353,536
82,373,147,791,837,737,099,225,129,670,497,061,391,352,260,647,104
92,643,871,745,652,043,632,824,104,595,348,162,111,992,169,136,063,475,200
102,945,479,346,671,522,648,704,114,696,170,451,658,709,411,132,712,736,787,285,248

A string is well-formed UTF-32

As before, the well-formedness constraint eliminates 2,048 single-code point characters from contention. And as before, we'll measure length in 4-byte code units.

Again, because of the mandatory leading and trailing double quotes, it makes sense to consider sequences of length N + 2:

a0 = 1 a1 = 1112030a0 a2 = 8a0 + 1112030a1 a3 = 8a1 + 1112030a2 a4 = 8a2 + 1112030a3 a5 = 8a3 + 1112030a4 aN = 234256aN-6 + 8aN-2 + 1112030aN-1 (N≥6)

Or:

( aN-5 aN-4 aN-3 aN-2 aN-1 aN ) = ( 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 234256 0 0 0 8 1112030 ) N ( 0 0 0 0 0 1 ) (N≥0)

A table of these values is as follows:

N Number of well-formed UTF-32 JSON string literals of length N + 2 4-byte code units
01
11,112,030
21,236,610,720,908
31,375,148,219,980,219,480
41,529,206,075,074,496,354,111,664
51,700,523,031,676,093,366,422,635,473,760
61,891,032,626,926,999,754,863,559,296,718,460,368
72,102,885,012,135,235,721,654,332,591,661,471,066,516,800
82,338,471,220,059,874,440,566,683,469,944,503,827,953,461,811,392
92,600,450,150,860,005,254,240,450,904,855,943,389,253,799,592,948,887,040
102,891,778,581,279,559,412,633,487,615,251,846,420,609,781,443,188,507,331,544,320

A string is well-formed UTF-16

With UTF-16, we find that of our 1,112,030 single code points, 63,454 are encoded using 1 2-byte code unit, and the remaining 1,048,576 are encoded using 2 2-byte code units. Once again, we ignore the leading and trailing double quote, and let N be the length in 2-byte code units.

The recurrence relation is:

a0 = 1 a1 = 63454a0 a2 = (1048576+8)a0 + 63454a1 a3 = (1048576+8)a1 + 63454a2 a4 = (1048576+8)a2 + 63454a3 a5 = (1048576+8)a3 + 63454a4 aN = 234256aN-6 + (1048576+8)aN-2 + 63454aN-1 (N≥6)

Or...

( aN-5 aN-4 aN-3 aN-2 aN-1 aN ) = ( 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 234256 0 0 0 1048584 63454 ) N ( 0 0 0 0 0 1 ) (N≥0)

Or...

N Number of well-formed UTF-16 JSON string literals of length N + 2 2-byte code units
01
163,454
24,027,458,700
3255,624,901,198,936
416,224,645,609,430,765,744
51,029,786,706,682,218,594,626,400
665,361,098,589,605,218,053,490,726,352
74,148,502,967,768,849,173,869,043,143,437,632
8263,307,643,919,008,038,076,654,608,599,857,813,696
916,712,273,291,072,690,979,058,233,712,497,751,374,130,688
101,060,736,689,594,217,702,509,763,533,509,518,271,813,265,409,280

A string is well-formed UTF-8

With UTF-8, we find that of our 1,112,030 single code points, 94 are encoded using 1 byte, 1,920 are encoded using 2 bytes, 61,440 are encoded using 3 bytes, and the remaining 1,048,576 are encoded using 4 bytes.

The recurrence relation is:

a0 = 1 a1 = 94a0 a2 = (1920+8)a0 + 94a1 a3 = 61440a0 + (1920+8)a1 + 94a2 a4 = 1048576a0 + 61440a1 + (1920+8)a2 + 94a3 a5 = 1048576a1 + 61440a2 + (1920+8)a3 + 94a4 aN = 234256aN-6 + 1048576aN-4 + 61440aN-3 + (1920+8)aN-2 + 94aN-1 (N≥6)

Or...

( aN-5 aN-4 aN-3 aN-2 aN-1 aN ) = ( 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 234256 0 1048576 61440 1928 94 ) N ( 0 0 0 0 0 1 ) (N≥0)

This works out to:

N Number of well-formed UTF-8 JSON string literals of length N + 2 bytes
01
194
210,764
31,254,488
4145,508,800
516,857,326,368
61,953,600,134,032
7226,407,391,417,664
826,238,584,628,724,416
93,040,814,652,976,950,272
10352,403,106,974,507,787,008

A string is a JavaScript string

The recurrence relation is:

a0 = 1 a1 = 65502a0 a2 = 8a0 + 65502a1 a3 = 8a1 + 65502a2 a4 = 8a2 + 65502a3 a5 = 8a3 + 65502a4 aN = 234256aN-6 + 8aN-2 + 65502aN-1 (N≥6)

Yielding the matrix:

( aN-5 aN-4 aN-3 aN-2 aN-1 aN ) = ( 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 234256 0 0 0 8 65502 ) N ( 0 0 0 0 0 1 ) (N≥0)

And the table:

N Number of JavaScript JSON string literals of length N + 2
01
165,502
24,290,512,012
3281,037,118,334,040
418,408,493,359,440,384,176
51,205,793,134,278,360,990,968,672
678,981,862,028,769,148,505,953,261,008
75,173,469,936,254,781,839,663,853,774,531,904
8338,872,628,396,415,616,291,815,943,065,196,746,944
922,196,834,946,609,775,188,384,848,454,598,539,973,455,360
101,453,937,085,383,814,521,560,913,586,107,661,518,929,482,499,328