Is there a way that I can instruct WCF to accept JSON that is formatted using either single quotes (as opposed to double quotes):
{
'foo': 'bar'
}
Or using non-quoted identifiers like so:
{
foo: 'bar'
}
As it is, it seems like JSON will only be accepted if it is formatted like so:
{
"foo": "bar"
}
Using either of the first two example results in a 400 (bad request).
The first two examples are invalid JSON texts.
http://www.ietf.org/rfc/rfc4627.txt
object = begin-object [ member *( value-separator member ) ]
end-object
member = string name-separator value
string = quotation-mark *char quotation-mark
quotation-mark = %x22 ; "
DataContractJsonSerializer always writes strict JSON.
At various points during deserialization (generally missing end tags for arrays or objects, or improper escaping, or improperly formatted numbers), it will accept incorrect, non-strict JSON.
However, I can tell you definitively that this is not one of those cases. DataContractJsonSerializer always requires double-quoted strings for JSON.
Hope this helps!
Related
A context entry & key are defined by the following grammar (cf DMN v1.2, page 111, Section 10.3.1.2)
60. context entry = key , ":", expression;
61. key = name | string literal;
Consider the following instance of a context object
{ "12" : "hello" }
How do I access "hello" from such an object?
Could this be an issue in the grammar? Not sure if this kind of accession is valid.
Accordingly to DMN specification, as "12" cannot be transformed into a legal name I concur with you, cannot be accessed with the dot operator.
But you can use the built-in function get value() as for the spec:
If key1 is not a legal name or for whatever reason one wishes to treat
the key as a string, the following syntax is allowed: get value(m,
"key1").
For example:
get value({ "12" : "hello" }, "12")
this is valid FEEL and would result in "hello".
I see no issue in the grammar.
I believe the only way to access this entry value is by using the built-in function.
Suppose we define a type alias say Message as:
type alias Message a =
{ code : String
, body : a
}
And later define a function readMessage as:
readMessage : Message () -> String
readMessage message =
...
The above example is from Elm tutorial and book says:
This function takes Message with an empty body. This is not the same
as any value, just an empty one.
Can someone please elaborate what exactly happens in above scenario and how compiler handles it.
Unless you really want to see the internal compiler representation of that, I think what's important here is the difference between any value and empty value.
Message a is a parametrized type with 1 parameter. You can read it as a template, e.g. wherever lowercase a appears in the definition of the Message it will be substituted with the concrete type (String, Int, etc).
So this is how function should look like if we want it to take a Message with String body:
readMessage : Message String -> String
readMessage message =
What happens here is that the type of body field is no longer an a but a String (a is substituted with String):
{ code : String
, body : String
}
The value of nothing (aka void or unit) in Elm is encoded as (). That's why a Message with empty body value looks like this:
{ code : String
, body : ()
}
But when we simply don't care about the body value we can just take a Message with any value:
readMessage : Message a -> String
readMessage message =
The lowercase a can be any lowercase string, we can make it more readable like this:
readMessage : Message any -> String
readMessage message =
But then we cannot really read message body, because we don't know the type of it (so we don't know how to read it).
Hope that helps.
The type Message () is an alias for the following record:
{ code : String
, body : ()
}
where the () type denotes a tuple without any items (also known as null tuple). There is only one value of such type and it is also written ().
Now, when we want to omit some field in a record, we cannot just not specify it – that would make the compiler rightly mad, see also The Billion Dollar Mistake. We need to tell the compiler that the value can be omitted.
One way we could do it is to use the Maybe type but if we made a list of messages that would allow us to include the body in some messages and omit it in others. This might not be what we want.
The alternative is to parametrize the Message type as you are doing in the question. This will allow us to have a messages with String bodies when reading the message, and with a different body type when we are not interested in the body.
In this case, we need to consider what the body type should be. While we could use an empty Strings for messages with omitted bodies, they would be easily confused with messages with empty bodies. We could also use Bool but then we would need to decide if we want to use True or False for the omitted value. Finally, we can use the null tuple; since it only has one possible value it is ideal for us.
There is actually one more possibility: we could create a type alias MessageWithoutBody = { code: String }. This is cleaner in some cases (especially when you need to omit more fields) but can be more verbose as you need to duplicate all the fields you want to keep.
If I have a blank JSON schema, such as
{}
and I try to validate the following data:
{
"hello": "world",
}
would validation be successful? (note the trailing comma).
I tried using everit json schema validator in java,
JSONObject rawSchema = new JSONObject(new JSONTokener("{}"));
Schema schema = SchemaLoader.load(rawSchema);
schema.validate(new JSONObject("{\"hello\" : \"world\",}"));
and it seems to validate.
Interestingly, some online validates this JSON
https://www.jsonschemavalidator.net/
whereas others don't
https://json-schema-validator.herokuapp.com/
The later uses a parser from Jackson before validating, perhaps that's the reason?
JSON Schema validates JSON. Technically, trailing commas are not valid JSON. However, many JSON ignore that and allow trailing commas. In general you're safer not having trailing commas in your JSON so you know it will work with all JSON parsers.
You are validating against the empty schema ({}). An empty schema means there are no constraints on what the value can be. An value that is valid JSON will be valid against this schema. Therefore, the only reason you would have validators reporting different results is if they disagree on whether it's valid JSON. If the validator uses a JSON parser that allows trailing commas, it will be valid, otherwise it will be invalid.
How do I replace multiple characters in a String?
Like Java's replaceAll(regex:replacement:) function.
str.replaceAll("[$,.]", "") //java code
This answer is very close but I want to change more than one character at the same time.
[$,.] is regex, which is the expected input for Java's replaceAll() method. Kotlin, however, has a class called Regex, and string.replace() is overloaded to take either a String or a Regex argument.
So you have to call .toRegex() explicitly, otherwise it thinks you want to replace the String literal [$,.]. It's also worth mentioning that $ in Kotlin is used with String templates, meaning in regular strings you have to escape it using a backslash. Kotlin supports raw Strings (marked by three " instead of one) which don't need to have these escaped, meaning you can do this:
str = str.replace("""[$,.]""".toRegex(), "")
In general, you need a Regex object. Aside using toRegex() (which may or may not be syntactical sugar), you can also create a Regex object by using the constructor for the class:
str = str.replace(Regex("""[$,.]"""), "")
Both these signal that your string is regex, and makes sure the right replace() is used.
If you're happy to work with regular expressions, then refer to the accepted answer here. If you're curious as to how you can achieve this without regular expressions, continue reading.
You can use the String.filterNot(predicate:) and Set.contains(element:) functions to define a String.removeAll extension function as follows:
/**
* #param charactersToRemove The characters to remove from the receiving String.
* #return A copy of the receiving String with the characters in `charactersToRemove` removed.
*/
fun String.removeAll(charactersToRemove: Set<Char>): String {
return filterNot { charactersToRemove.contains(it) }
}
You would call on this function as follows: myString.removeAll(setOf('$', '.'))
I need to parse Javascript object definitions using Objective C.
Note that I'm NOT deserialising JSON. I need to parse bits of actual javascript source code. For example, something like this:
{ name : "Peter", phone : "" }
Note that unlike JSON, the 'name' and 'phone' don't have to be in quotes. This causes NSJSONSerialisation to fail immediately, so pretending that it's just JSON isn't going to work.
I don't suppose anyone has a solution that doesn't involve me writing an entire parser?