string(JSON ... SET ...) example

I’ve found the string(JSON ... GET) to be a great way to separate project data (urls, md5s, git tags) from code by reading in a json file with those parameters. Especially useful for projects with lots of FetchContent/ExternalProject.

A general observation: I’ve been thinking about applications for string(JSON ... SET) to store build metadata without using cmake-server or trace mode. Say for reproducibility and storing of a few key parameters. It looks like this can act like a mappable (dictionary) almost. I came up with this toy example of incrementally building up JSON string as one might with a dictionary in Python.

set(dat "{}")
set(fake "abc" "def" "xyz")

foreach(i RANGE 1)

  string(JSON dat SET ${dat} key${i} "{}")
  foreach(j RANGE 2)
    list(GET fake ${j} x)
    string(JSON dat SET ${dat} key${i} prp${j} \"${x}\")
  endforeach()

endforeach()

message(${dat})

results in:

{
  "key0" :
  {
    "prp0" : "abc",
    "prp1" : "def",
    "prp2" : "xyz"
  },
  "key1" :
  {
    "prp0" : "abc",
    "prp1" : "def",
    "prp2" : "xyz"
  }
}