- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在使用 Google 的 go-jsonnet评估一些 jsonnet 文件的库。
我有一个函数,就像这样,它呈现一个 Jsonnet 文档:
// Takes a list of jsonnet files and imports each one and mixes them with "+"
func renderJsonnet(files []string, param string, prune bool) string {
// empty slice
jsonnetPaths := files[:0]
// range through the files
for _, s := range files {
jsonnetPaths = append(jsonnetPaths, fmt.Sprintf("(import '%s')", s))
}
// Create a JSonnet VM
vm := jsonnet.MakeVM()
// Join the slices into a jsonnet compat string
jsonnetImport := strings.Join(jsonnetPaths, "+")
if param != "" {
jsonnetImport = "(" + jsonnetImport + ")" + param
}
if prune {
// wrap in std.prune, to remove nulls, empty arrays and hashes
jsonnetImport = "std.prune(" + jsonnetImport + ")"
}
// render the jsonnet
out, err := vm.EvaluateSnippet("file", jsonnetImport)
if err != nil {
log.Panic("Error evaluating jsonnet snippet: ", err)
}
return out
}
此函数当前返回一个字符串,因为 jsonnet EvaluateSnippet
函数返回一个字符串。
我现在要做的是使用 go-prettyjson 呈现结果 JSON图书馆。但是,因为我传入的 JSON 是一个字符串,所以它无法正确呈现。
那么,一些问题:
最佳答案
Can I convert the returned JSON string to a JSON type, without knowing beforehand what struct to marshal it into
是的。这很容易:
var jsonOut interface{}
err := json.Unmarshal([]byte(out), &jsonOut)
if err != nil {
log.Panic("Invalid json returned by jsonnet: ", err)
}
formatted, err := prettyjson.Marshal([]byte(jsonOut))
if err != nil {
log.Panic("Failed to format jsonnet output: ", err)
}
更多信息在这里:https://blog.golang.org/json-and-go#TOC_5 .
Is there an option, function or method I'm missing here to make this easier?
是的。 go-prettyjson 库有一个 Format
函数,可以为您进行解码:
formatted, err := prettyjson.Format([]byte(out))
if err != nil {
log.Panic("Failed to format jsonnet output: ", err)
}
can I render the json in a pretty manner some other way?
取决于你对漂亮的定义。 Jsonnet 通常在单独的行上输出对象的每个字段和每个数组元素。这通常被认为是 pretty-print (而不是将所有内容都放在同一行上并使用最少的空白以节省几个字节)。我想这对你来说还不够好。您可以在 jsonnet 中编写您自己的 list ,根据您的喜好对其进行格式化(请参阅 std.manifestJson 作为示例)。
关于json - 使用 go-jsonnet 返回纯 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51091711/
我正在使用 jsonnet 读取由数组组成的值。我想修改该数组中的第一个元素以添加一个值。数据结构如下所示: { "my_value": [ { "env": "s
我不明白如何最好地参数化 Jsonnet 文件,以便我可以从 bash 和另一个 Jsonnet 文件调用同一个文件。 假设我有一个名为 template.jsonnet 的简单模板: { //
有什么方法可以导入 .yaml jsonnet 中的文件? 我发现jsonnet支持导入.json并且还有一个本地 importstr()功能,但似乎不支持 .yaml ? 我想要做: local f
如何附加到现有列表? 这是无效的: local list = ['a', 'b', 'c']; local list = list + ['e']; 最佳答案 您遇到的情况是由于本地人在 jsonne
有没有办法在 jsonnet 中打印对象?这主要用于调试目的。 我正在使用 error打印对象,但终止程序执行。 local obj = [ { myKey: 2, }]; error 'Prin
有没有办法从 jsonnet 调用 golang 函数? 现在有一个 jsonnet 的 go 端口,例如 ksonnet 正在添加 custom native functions我想知道是否有办法用
Jsonnet 的文档提到 + 运算符可用于 inheritance。 ,或者,如教程中所述,到 combine objects : { a: 1, b: 2, } + { a: 3 }
我有一个接受 bool 值作为参数的 jsonnet 函数。假设我有一个名为 deploy.jsonnet 的 jsonnet 文件: function (image='', isReady) { l
我正在使用 Asp MVC 3 应用程序。 我有一个枚举: public enum EmployeesOptions { John = 1, Peter = 2
我几乎尝试了 2 天,到处搜索以组织下一次。这是我的 json 示例(使用 WebClient => DownloadStringCompletedEventHandler 获取): {"order_
快速提问: 在 JSONNet 中——如何获取 bool true/false 以序列化为 bool 1/0 我可以看到我们如何处理空值以及所有似乎无法找到如何执行此操作的方法。 这可能吗? 最佳答案
我能够生成面板 JSON 以像这样配置 Grafana 仪表板: jsonnet -J ~/grafana/grafonnet-lib mydash.jsonnet > mydash.json 不过,
我想用多行创建一个 kubernetes 配置映射,比如这种 yaml: apiVersion: v1 kind: ConfigMap metadata: name: nifi-bootstrap
我正在使用 jsonnet 构建 Python 代码将使用的 json 对象,使用 bindings 从 Python 调用 jsonnet .我想设置我的目录结构,以便 jsonnet 文件位于相对
我正在尝试反序列化一个 JSON,其中包含格式为 2017-10-26 13:32:11 Etc/GMT 的日期。 NodaTime与 Json.NET 结合使用时似乎支持这一点和 NodaTime.
我正在使用 Google 的 go-jsonnet评估一些 jsonnet 文件的库。 我有一个函数,就像这样,它呈现一个 Jsonnet 文档: // Takes a list of jsonnet
谁能帮我举个 Jsonnet 的 std.lines(arr) 函数的例子?我正在尝试创建一个 bash 脚本来使用数组中的值克隆多个 git 存储库。我的数组结构如下。 git repo :[ {
我已经从 https://github.com/grafana/grafonnet-lib 克隆了 grafonnet-lib链接并创建了一个示例 jsonnet 文件,以将其转换为 grafana
Jsonnet的 std.mergePatch 工具RFC7396 ,但在我幼稚的测试中,我没有发现它的行为方式与 + 之间的区别。运算符(operator);例如+运营商尊重 x+句法。 std.m
我试图在 json NET 中的 DeserializeObject 期间设置类属性将电子邮件值设置为 UpperCase 但它在构造函数中不起作用 这是我的代码: //账号类 public cla
我是一名优秀的程序员,十分优秀!