简体   繁体   English

将 JSON 数据嵌入 YAML 文件

[英]Embedding JSON Data into YAML file

I am writing a fixture for my table.我正在为我的桌子写一个固定装置。 And a one of the coloums takes in a JSON string as a value.其中一个列将 JSON 字符串作为值。

The problem is the fixture is not loading failing as:问题是夹具没有加载失败,因为:

Fixture::FormatError: a YAML error occurred parsing /home/saurajeet/code/dcbox/test/fixtures/hardware.yml. Please note that YAML must be consistently indented using spaces. Tabs are not allowed. Please have a look at http://www.yaml.org/faq.html
The exact error was:
  ArgumentError: syntax error on line 145, col 73: `  portslist: [{"name":"ob1","port_num":0,"port_type":"network"},{"name":"ob2","port_nu'.....

Any solutions to this.对此的任何解决方案。

我相信将其引入引号应该可以解决问题:

portslist: '[{"name":"ob1","port_type" ... }]'

clarkevans' comment on the accepted answer suggested a better answer for long bits of JSON, because you can wrap the lines. clarkevans对接受的答案的评论为长篇JSON提供了更好的答案,因为你可以包装这些行。 I looked up the block scalar syntax he mentioned, and thought I would include an example here: 我查看了他提到的块标量语法,并认为我会在这里包含一个例子:

portslist: >
  [{"name":"ob1","port_num":0,"port_type":"network"},
  {"name":"ob2","port_nu...

The || is also possible.也是可能的。 For example.例如。

MyObject:
  type: object
  example: |
    {
        "id": 54,
        "manufacturer": "ACME",
        "location": "New York",
        "createdAt": "2012-10-01 07:42:35.825565",
        "description": "test",
    }

For the sake of being complete: In case you're using ActiveRecord::Store , you can load your data simply using YAML representation of the same data, even if it is a JSON store: 为了完整起见:如果您正在使用ActiveRecord::Store ,您可以使用相同数据的YAML表示来加载数据,即使它是JSON存储:

one:
  portslist:
    - 
      name: 'ob1'
      port_num: 0
      port_type: 'network'
    - 
      name: 'ob2'
      port_num: 1
      port_type: 'network'

If you have the string, you can use as simple as Vlad Khomich mentioned: 如果你有字符串,你可以像Vlad Khomich提到的那样简单:

portslist: '[{"name":"ob1","port_num":0,"port_type":"network"},...]'

If you are using ERB and have an object, you can use to_json and inspect to escape to a JSON string: 如果您正在使用ERB并拥有一个对象,则可以使用to_json并检查以转义为JSON字符串:

portslist: <%= [{name: 'ob1', port_num: 0, port_type: 'network'},...].to_json.inspect %>

And if you have a large JSON specification, you can store it in a separated file and load using Ruby, so you can keep your YAML file clean: 如果你有一个大的JSON规范,你可以将它存储在一个单独的文件中并使用Ruby加载,这样你就可以保持你的YAML文件干净:

portslist: <%= File.read('/path/to/file.json').inspect %>

In my table, the column stripe_connect is of type JSONB . 在我的表中,列stripe_connect的类型为JSONB In the fixture, here is what worked. 在夹具中,这是有效的。 Note that the outer single-quotes are necessary, but square brackets are not. 请注意,外部单引号是必需的,但方括号不是。 Everything between the single quotes is one long line. 单引号之间的所有内容都是一条长线。

 stripe_connect: '{"scope":"read_write", "livemode":false, "token_type":"bearer", "access_token":"sk_test_madeupvalue", "refresh_token":"rt_Ae29madeupvalueyX", "stripe_user_id":"acct_103yZabcdefg", "stripe_publishable_key":"pk_test_0HEOmadeupvalue"}'

Check out the wonderful interactive documentation and demo provided by yaml-multiline.info .查看yaml-multiline.info提供的精彩互动文档和演示。 I've archived the text content below:我已将以下文本内容存档:

There are two types of formats that YAML supports for strings: block scalar and flow scalar formats. YAML 支持两种类型的字符串格式:块标量流标量格式。 ( Scalars are what YAML calls basic values like numbers or strings, as opposed to complex types like arrays or objects.) Block scalars have more control over how they are interpreted, whereas flow scalars have more limited escaping support. 标量是 YAML 所称的基本值,如数字或字符串,而不是复杂类型,如 arrays 或对象。)块标量可以更好地控制它们的解释方式,而流标量对 escaping 的支持更有限。

Block Scalars块标量

A block scalar header has three parts:块标量 header 具有三个部分:

Block Style Indicator : The block style indicates how newlines inside the block should behave.块样式指示器块样式指示块内的换行符应如何表现。 If you would like them to be kept as newlines, use the literal style, indicated by a pipe ( | ).如果您希望将它们保留为换行符,请使用由 pipe ( | ) 指示的文字样式。 If instead you want them to be replaced by spaces, use the folded style, indicated by a right angle bracket ( > ).如果您希望将它们替换为空格,请使用折叠样式,由直角括号 ( > ) 指示。 (To get a newline using the folded style, leave a blank line by putting two newlines in. Lines with extra indentation are also not folded.) (要使用折叠样式获得换行符,请通过放入两个换行符来留下一个空白行。带有额外缩进的行也不折叠。)

Block Chomping Indicator : The chomping indicator controls what should happen with newlines at the end of the string. Block Chomping Indicator : chomping 指示器控制字符串末尾的换行符应该发生什么。 The default, clip , puts a single newline at the end of the string.默认值clip会在字符串末尾放置一个换行符。 To remove all newlines, strip them by putting a minus sign ( - ) after the style indicator.要删除所有换行符,请在样式指示符后添加减号 ( - ) 来去除它们。 Both clip and strip ignore how many newlines are actually at the end of the block; clip 和 strip 都忽略块末尾实际有多少换行符; to keep them all put a plus sign ( + ) after the style indicator.为了它们都在样式指示符后面加上一个加号 ( + )。

Indentation Indicator : Ordinarily, the number of spaces you're using to indent a block will be automatically guessed from its first line.缩进指示符:通常,您用于缩进一个块的空格数将自动从其第一行猜测。 You may need a block indentation indicator if the first line of the block starts with extra spaces.如果块的第一行以额外的空格开头,则可能需要块缩进指示符 In this case, simply put the number of spaces used for indentation (between 1 and 9) at the end of the header.在这种情况下,只需将用于缩进的空格数(1 到 9 之间)放在 header 的末尾即可。

Flow Scalars流量标量

(Ed. reformatted for SO) (Ed. 为 SO 重新格式化)

Double-quoted双引号

Escapes (like \n ) work.转义(如\n )工作。 In addition, newlines can be escaped to prevent them from being converted to a space.此外,可以转义换行符以防止它们被转换为空格。 Newlines can also be added by leaving a blank line.也可以通过留空行来添加换行符。 Leading whitespace on lines is ignored.行上的前导空格被忽略。

Single-quoted单引号

Escapes (like \n ) don't do anything.转义(如\n )不做任何事情。 Newlines can be added by leaving a blank line.可以通过留空行来添加换行符。 Leading whitespace on lines is ignored.行上的前导空格被忽略。

Plain清楚的

Escapes (like \n ) don't do anything.转义(如\n )不做任何事情。 Newlines can be added by leaving a blank line.可以通过留空行来添加换行符。 Additional leading whitespace is ignored.额外的前导空格被忽略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM