I'm making my first videogame in Unity and I was messing around with storing data in JSON files. More specifically, language localization files. Each file stores key-value pairs of strings. Keys are grouped in categories (like "menu.pause.quit").
Now, since each language file is essentially going to have the same keys (but different values), it would be nice if VS code recognized and helped me write these keys with tooltips.
For example, if you open settings.json in VS code, if you try to write something, there's some kind of autocompletion going on:
How does that work?
The auto completion for json files is done with json schemas. It is described in the documentation:
https://code.visualstudio.com/docs/languages/json#_json-schemas-and-settings
Basically you need to create a json schema which describes your json file and you can map it to json files via your user or workspace settings:
// settings.json
{
// ... other settings
"json.schemas": [
{
"fileMatch": [
"/language-file.json"
],
"url": "./language-file-schema.json"
}
]
}
This will provide auto completion for language-file.json (or any other file matched by the pattern) based on language-file-schema.json(in your workspace folder, but you can also use absolute paths)
The elements of fileMatch may be patterns which will match against multiple files.
Related
What is the completion support in special files like launch.json powered by in VS Code? Is it only the JSON schema, or is there a more complex completion service behind it? I'm particularly interested in completions like the one below (I did not see $ or { listed as JSON trigger characters...):
I am working with JSON schema for a file, which keeps a set of variables we are using to define our configuration, which will be executed through Ansible. Importantly, we are using JSON schema to validate YAML files.
So far everything goes well. However, I have this challenge.
I have a file called common.yml and other called domain.yml. Inside domain.yml we have a property called domain_root, which depends on a property called common_dir, which is inside common.yml and I have not found any documentation on how to define a dependency when the property is in place, but in another file.
By the way, dependencies in the same file are working without issues.
"dependencies": {
"domain_home": ["domain_parent_dir", "domain_name"],
"domain_libraries":["domain_home"],
"logs_directory":["oracle_user", "domain_name"],
}
Please, if you have any clues, kindly help me.
Best regards,
RCC
You cannot. JSON Schema actually doesn't work with files at all. JSON Schema implementations may load a file to get to the JSON, but JSON Schema knows nothing about files in a filesystem.
In stead, consider combining your multiple files into a single file for validation purposes.
This doesn't help if you want validation in-editor, but could help if you only need validation as part of a continuious intergration (CI) process.
I have a system where we collect a lot of JSON configuration from different parties to configure our overall service.
The repository looks like a directory of formatted JSON files. For example foo.json:
{
"id": "3bd0e397-d8cc-46ff-9e0d-26fa078a37f3",
"name": "Example",
"logo": "https://example/foo.png"
}
We have a pipeline whereby the owner of foo.json can overwrite this file by committing a new file at any time, since fast updates are required.
However we require unfortunately to skip whole files or override some values for various $reasons.
Hence we commit something like touch foo.json.skip when we want the file to be skipped before publishing. And similarly, we have a foo.json.d/override.json to perhaps override the logo because it's poorly formatted or something.
Is there a name for this sort of JSON pipeline that we have? It's inspired by systemd configuration, but maybe system configuration was inspired by something else?
I'm new to Scala and Scala.js and I want to experiment with handling JSON data. I'd like to simulate a server response by returning the content of a JSON file local to my Scala.js project, parse it and work with the data. What would be the best way to do so? Where should I place these files in my project tree, and how would I get their content?
Say that I have a file called myJSON.json containing something like
[
{
"ress": "AR",
"lastDate": "2017-10-27 09:19:18"
},
{
"ress": "JIM",
"lastDate": "2017-10-27 06:57:15"
},
{
"ress": "JOE",
"lastDate": "2017-09-29 11:57:39"
}
]
Can I place this file somewhere in my project so that I can read this file and then parse its content to use it somehow (could be displayed in the browser, logged to the console, etc...)? I guess I could use a tool such as scala-js or something similar for parsing, but accessing the file content in the first place is what I try to figure out.
Note that I'm using scala-js.
Thanks in advance!
Like others said above, Javascript that runs in the browser in general can't access the local filesystem. There are some exceptions:
The File API lets you access files that the user has selected in the UI using <input type="file" /> or drag-and-dropped into the browser window.
The Filesystem API lets you access files the way you seem to want, but it is non-standard and is not supported in most browsers. It also seems that Scala.js has no typings for it, but I'm not sure.
scala-js-dom has typings for the File API that you can use – search for File and FileList types in its source. Its API mirrors the Javascript API, so you will need to look for how exactly to do this in JS. Then translating it into Scala.js will be easy (or at least a different question).
If the File API does not work for your use case, another option is to use something like json-server to easily serve your JSON files on localhost via HTTP.
I want to use CouchBase to store lots of data. I have that data in the form:
[
{
"foo": "bar1"
},
{
"foo": "bar2"
},
{
"foo": "bar3"
}
]
I have that in a json file that I zipped into data.zip. I then call:
cbdocloader.exe -u Administrator -p **** -b mybucket C:\data.zip
However, this creates a single item in my bucket; not three as I expected. This actually makes sense as I should be able to store arrays and I did not "tell" CouchBase to expect multiple items instead of one.
The temporary solution I have is to split every items in multiplejson files, then add the lot of them in a single zip file and call cbdocloader again. The problem is that I might have lots of these entries and creating all the files might take too long. Also, I saw in the doc that cbdocloader uses the filename as a key. That might be problematic in my case...
I obviously missed a step somewhere but couldn't find what in the documentation. How should I format my json file?
You haven't missed any steps. The cbdocloader script is very limited at the moment. Couchbase will be adding a cbimport and cbexport tool in the near future that will allow you to add json files with various formats (including the one you mentioned). In the meantime you will need to use the current workaround you are using to get your data loaded.