简体   繁体   English

具有保留字符的Typesafe ConfigFactory错误

[英]Typesafe ConfigFactory error with reserved characters

Hi I am trying to load configuration from a String in Java as follows: 嗨,我正在尝试从Java中的字符串加载配置,如下所示:

@Test
public void testIllegalCharacter(){
    String input = "prop=\\asd";
    Config conf = ConfigFactory.parseString(input);
}

The code above produces the following error: 上面的代码产生以下错误:

com.typesafe.config.ConfigException$Parse: String: 1: Expecting a value but got wrong token: '\' (Reserved character '\' is not allowed outside quotes) (if you intended '\' (Reserved character '\' is not allowed outside quotes) to be part of a key or string value, try enclosing the key or value in double quotes, or you may be able to rename the file .properties rather than .conf)

I understand I have an illegal character in my String. 我知道我的字串中含有非法字符。 Although how do I find the full set of illegal characters? 虽然我如何找到全套非法字符?

If I (for example) convert this String into a Properties object and then parse it with ConfigFactory.parseProperties I can see the value "\\\\asd" in resolved as "asd". 如果我(例如)将该String转换为Properties对象,然后使用ConfigFactory.parseProperties进行解析,则可以看到解析为"asd".的值"\\\\asd" "asd". So there must be some some sanitising going on in the typesafe library, I wish I could call that sanitisation myself, but I cannot see how. 因此,必须有一些一些消毒的类型安全库的时候,我希望我能称之为禁制自己,但我看不出。 Parsing to Properties is not a viable solution as the configuration could be composed by Objects or Lists too. 解析为属性不是可行的解决方案,因为配置也可以由对象或列表组成。

Has anyone have any suggestion how to solve this issue? 有没有人对如何解决这个问题有任何建议?

Alternatively can someone point out all the reserved characters set? 或者,有人可以指出所有保留字符集吗?

Many thanks 非常感谢

If I understand the error message correctly, you should put quotes around you special characters, eg like this: 如果我正确理解错误消息,则应在特殊字符两边加上引号,例如:

"prop=\"\\asd\"";

Not sure why you's want a property definition with a backslash a ('\\a') in it, but I guess I don't need to know :-) 不知道为什么要在其中加上反斜杠('\\ a')的属性定义,但是我想我不需要知道:-)

I think I might have found the answer. 我想我可能已经找到了答案。 I need to set the ConfigParseOptions.defaults().setSyntax(ConfigSyntax.PROPERTIES) 我需要设置ConfigParseOptions.defaults().setSyntax(ConfigSyntax.PROPERTIES)

Which works for the test below: 适用于以下测试:

@Test
    public void test(){
        String input = "prop=C:/MyDocuments/mydir";
        Config conf = ConfigFactory.parseString(input, ConfigParseOptions.defaults().setSyntax(ConfigSyntax.PROPERTIES));
        assertEquals("C:/MyDocuments/mydir", conf.getAnyRef("prop"));
    }

But will not work for the test with backslashes 但不适用于带有反斜杠的测试

@Test
    public void test(){
        String input = "prop=C:\\MyDocuments\\mydir";
        Config conf = ConfigFactory.parseString(input, ConfigParseOptions.defaults().setSyntax(ConfigSyntax.PROPERTIES));
        assertEquals("C:\\MyDocuments\\mydir", conf.getAnyRef("prop"));
    } 

Which fails: 失败:

org.junit.ComparisonFailure: 
Expected :C:\MyDocuments\mydir
Actual   :C:MyDocumentsmydir

So I am not sure this is the definitive answer... 所以我不确定这是否是肯定的答案...

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

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