Alex
Alex

Reputation: 169

Deserialize JSON Map in a constant-foldable way C#

I need to pass a string representation of a <string,string> map into my U-SQL program, and deserialize it into a C# Dictionary<string,string> so I can then turn it to a U-SQL SqlMap. I need to do it in a constant-foldable way. My most recent attempt:

DECLARE @MapStr string = "{\"key\": \"val\"}";
DECLARE CONST @map = new SqlMap<string,string>(JsonConvert.DeserializeObject<Dictionary<string, string>>(@MapStr));

Fails with "E_CSC_USER_EXPRESSIONNOTCONSTANTFOLDABLE: Expression cannot be constant folded."

I have found numerous ways to deserialize a string into a map, but none so far were constant foldable. I can't find a list of constant-foldable c# expressions, which would also be helpful here.

Upvotes: 0

Views: 306

Answers (2)

Alex
Alex

Reputation: 169

In case anyone else was curious, I believe this is not possible:

  • Only String and built-in type expressions are constant-foldable, excluding Objects (such as arrays and maps). (see other answer for links on this)
  • There are no built-in expressions or String expressions that evaluate to a SqlMap
  • Therefore there are no constant-foldable expressions that evaluate to a SqlMap

For my specific purposes, I was able to use a configuration file that created CONST SqlMaps and use this, but that would not be a possibility if we had more than a finite set of possible inputs.

Upvotes: 0

Guru Stron
Guru Stron

Reputation: 143083

From the docs:

... can be computed at compile time (so called constant-foldable expressions)

In C# constants are defined as:

immutable values which are known at compile time and do not change for the life of the program. Constants are declared with the const modifier. Only the C# built-in types (excluding System.Object) may be declared as const. User-defined types, including classes, structs, and arrays, cannot be const.

So it seems you can use only what can be defined as constant in C#, so you are limited to expressions of built-in types (excluding System.Object).

Upvotes: 0

Related Questions