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.
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:
Or...
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 |
|---|---|
| 0 | 1 |
| 1 | 1,114,078 |
| 2 | 1,241,169,790,092 |
| 3 | 1,382,759,957,415,027,800 |
| 4 | 1,540,502,447,846,948,699,689,136 |
| 5 | 1,716,239,886,103,494,993,111,593,479,008 |
| 6 | 1,912,025,099,842,733,514,518,553,429,504,021,968 |
| 7 | 2,130,145,099,196,322,787,476,628,927,580,135,513,353,536 |
| 8 | 2,373,147,791,837,737,099,225,129,670,497,061,391,352,260,647,104 |
| 9 | 2,643,871,745,652,043,632,824,104,595,348,162,111,992,169,136,063,475,200 |
| 10 | 2,945,479,346,671,522,648,704,114,696,170,451,658,709,411,132,712,736,787,285,248 |
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:
Or:
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 |
|---|---|
| 0 | 1 |
| 1 | 1,112,030 |
| 2 | 1,236,610,720,908 |
| 3 | 1,375,148,219,980,219,480 |
| 4 | 1,529,206,075,074,496,354,111,664 |
| 5 | 1,700,523,031,676,093,366,422,635,473,760 |
| 6 | 1,891,032,626,926,999,754,863,559,296,718,460,368 |
| 7 | 2,102,885,012,135,235,721,654,332,591,661,471,066,516,800 |
| 8 | 2,338,471,220,059,874,440,566,683,469,944,503,827,953,461,811,392 |
| 9 | 2,600,450,150,860,005,254,240,450,904,855,943,389,253,799,592,948,887,040 |
| 10 | 2,891,778,581,279,559,412,633,487,615,251,846,420,609,781,443,188,507,331,544,320 |
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:
Or...
Or...
| N | Number of well-formed UTF-16 JSON string literals of length N + 2 2-byte code units |
|---|---|
| 0 | 1 |
| 1 | 63,454 |
| 2 | 4,027,458,700 |
| 3 | 255,624,901,198,936 |
| 4 | 16,224,645,609,430,765,744 |
| 5 | 1,029,786,706,682,218,594,626,400 |
| 6 | 65,361,098,589,605,218,053,490,726,352 |
| 7 | 4,148,502,967,768,849,173,869,043,143,437,632 |
| 8 | 263,307,643,919,008,038,076,654,608,599,857,813,696 |
| 9 | 16,712,273,291,072,690,979,058,233,712,497,751,374,130,688 |
| 10 | 1,060,736,689,594,217,702,509,763,533,509,518,271,813,265,409,280 |
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:
Or...
This works out to:
| N | Number of well-formed UTF-8 JSON string literals of length N + 2 bytes |
|---|---|
| 0 | 1 |
| 1 | 94 |
| 2 | 10,764 |
| 3 | 1,254,488 |
| 4 | 145,508,800 |
| 5 | 16,857,326,368 |
| 6 | 1,953,600,134,032 |
| 7 | 226,407,391,417,664 |
| 8 | 26,238,584,628,724,416 |
| 9 | 3,040,814,652,976,950,272 |
| 10 | 352,403,106,974,507,787,008 |
The recurrence relation is:
Yielding the matrix:
And the table:
| N | Number of JavaScript JSON string literals of length N + 2 |
|---|---|
| 0 | 1 |
| 1 | 65,502 |
| 2 | 4,290,512,012 |
| 3 | 281,037,118,334,040 |
| 4 | 18,408,493,359,440,384,176 |
| 5 | 1,205,793,134,278,360,990,968,672 |
| 6 | 78,981,862,028,769,148,505,953,261,008 |
| 7 | 5,173,469,936,254,781,839,663,853,774,531,904 |
| 8 | 338,872,628,396,415,616,291,815,943,065,196,746,944 |
| 9 | 22,196,834,946,609,775,188,384,848,454,598,539,973,455,360 |
| 10 | 1,453,937,085,383,814,521,560,913,586,107,661,518,929,482,499,328 |