Skip to content

Commit

Permalink
17490: Fixes json parsing issues when the document is a single scalar (
Browse files Browse the repository at this point in the history
…#5)

Fixes json parsing issues when the document is a single scalar
Corrects documentation for mutate and mutate_entities
  • Loading branch information
howsohazard authored Sep 14, 2023
1 parent ae403ac commit e6277a8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions docs/language.js
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,7 @@ var data = [
"parameter" : "mutate * node [number mutation_rate] [assoc mutation_weights] [assoc operation_type]",
"output" : "*",
"new value" : "new",
"description" : "Evaluates to a mutated version of the input node. The value specified in mutation_rate, from 0.0 to 1.0 and defaulting to 0.00001, indicates the probability that any node will experience a mutation. The parameter mutation_weights is an assoc where the keys are the allowed opcode names and the values are the probabilities that each opcode would be chosen; if null or unspecified, it defaults to all opcodes each with their own default probability. The operation_type is an assoc where the keys are mutation operations and the values are the probabilities that the operations will be performed. The operations can consist of the strings change_type, delete, insert, swap_elements, deep_copy_elements, delete, and change_label.",
"description" : "Evaluates to a mutated version of the input node. The value specified in mutation_rate, from 0.0 to 1.0 and defaulting to 0.00001, indicates the probability that any node will experience a mutation. The parameter mutation_weights is an assoc where the keys are the allowed opcode names and the values are the probabilities that each opcode would be chosen; if null or unspecified, it defaults to all opcodes each with their own default probability. The operation_type is an assoc where the keys are mutation operations and the values are the probabilities that the operations will be performed. The operations can consist of the strings change_type, delete, insert, swap_elements, deep_copy_elements, delete_elements, and change_label.",
"example" : "(print (mutate\n (lambda (list 1 2 3 4 5 6 7 8 9 10 11 12 13 14 (assoc \"a\" 1 \"b\" 2)))\n0.4))\n"
},

Expand Down Expand Up @@ -1156,7 +1156,7 @@ var data = [
"output" : "id",
"permissions" : "e",
"new value" : "new",
"description" : "Creates a mutated version of the entity specified by entity1 like mutate. Returns the id of a new entity created contained by the entity that ran it. The value specified in mutation_rate, from 0.0 to 1.0 and defaulting to 0.00001, indicates the probability that any node will experience a mutation. Uses entity2 as the optional destination via an internal call to create_contained_entity. The parameter mutation_weights is an assoc where the keys are the allowed opcode names and the values are the probabilities that each opcode would be chosen; if null or unspecified, it defaults to all opcodes each with their own default probability. The operation_type is an assoc where the keys are mutation operations and the values are the probabilities that the operations will be performed. The operations can consist of the strings change_type, delete, insert, swap_elements, deep_copy_elements, delete, and change_label.",
"description" : "Creates a mutated version of the entity specified by entity1 like mutate. Returns the id of a new entity created contained by the entity that ran it. The value specified in mutation_rate, from 0.0 to 1.0 and defaulting to 0.00001, indicates the probability that any node will experience a mutation. Uses entity2 as the optional destination via an internal call to create_contained_entity. The parameter mutation_weights is an assoc where the keys are the allowed opcode names and the values are the probabilities that each opcode would be chosen; if null or unspecified, it defaults to all opcodes each with their own default probability. The operation_type is an assoc where the keys are mutation operations and the values are the probabilities that the operations will be performed. The operations can consist of the strings change_type, delete, insert, swap_elements, deep_copy_elements, delete_elements, and change_label.",
"example" : "(create_entities\n \"MutateEntity\"\n (lambda (list 1 2 3 4 5 6 7 8 9 10 11 12 13 14 (assoc \"a\" 1 \"b\" 2)))\n)\n(mutate_entity \"MutateEntity\" 0.4 \"MutatedEntity\")\n(print (retrieve_entity_root \"MutatedEntity\"))"
},

Expand Down
30 changes: 30 additions & 0 deletions src/Amalgam/importexport/FileSupportJSON.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ EvaluableNode *JsonToEvaluableNodeRecurse(EvaluableNodeManager *enm, simdjson::o
}

case simdjson::ondemand::json_type::boolean:
{
if(element.get_bool())
return enm->AllocNode(ENT_TRUE);
else
return enm->AllocNode(ENT_FALSE);
}

case simdjson::ondemand::json_type::null:
return nullptr;
Expand Down Expand Up @@ -277,6 +279,34 @@ EvaluableNode *EvaluableNodeJSONTranslation::JsonToEvaluableNode(EvaluableNodeMa
auto json_padded = simdjson::padded_string(json_str);
auto json_top_element = json_parser.iterate(json_padded);

//simdjson needs special handling if the top element is a scalar
if(json_top_element.is_scalar())
{
switch(json_top_element.type())
{
case simdjson::ondemand::json_type::number:
return enm->AllocNode(json_top_element.get_double());

case simdjson::ondemand::json_type::string:
{
std::string_view str_view = json_top_element.get_string();
std::string str(str_view);
return enm->AllocNode(ENT_STRING, str);
}

case simdjson::ondemand::json_type::boolean:
{
if(json_top_element.get_bool())
return enm->AllocNode(ENT_TRUE);
else
return enm->AllocNode(ENT_FALSE);
}

default:
return nullptr;
}
}

try
{
return JsonToEvaluableNodeRecurse(enm, json_top_element);
Expand Down

0 comments on commit e6277a8

Please sign in to comment.