@@ -369,6 +369,8 @@ private string GetOrCreate(
369369 Create ( column , tableParameters ) ;
370370 }
371371
372+ CreateJsonElements ( table , tableParameters ) ;
373+
372374 CreateAnnotations (
373375 table ,
374376 Generate ,
@@ -406,6 +408,8 @@ private string Create(
406408 Create ( column , tableParameters ) ;
407409 }
408410
411+ CreateJsonElements ( table , tableParameters ) ;
412+
409413 CreateAnnotations (
410414 table ,
411415 Generate ,
@@ -449,6 +453,8 @@ private string GetOrCreate(
449453 Create ( column , viewParameters ) ;
450454 }
451455
456+ CreateJsonElements ( view , viewParameters ) ;
457+
452458 CreateAnnotations (
453459 view ,
454460 Generate ,
@@ -469,6 +475,135 @@ private string GetOrCreate(
469475 public virtual void Generate ( IView view , CSharpRuntimeAnnotationCodeGeneratorParameters parameters )
470476 => GenerateSimpleAnnotations ( parameters ) ;
471477
478+ private void CreateJsonElements (
479+ ITableBase table ,
480+ CSharpRuntimeAnnotationCodeGeneratorParameters parameters )
481+ {
482+ foreach ( var column in table . Columns )
483+ {
484+ if ( column . JsonElement == null )
485+ {
486+ continue ;
487+ }
488+
489+ var code = Dependencies . CSharpHelper ;
490+ var mainBuilder = parameters . MainBuilder ;
491+ AddNamespace ( typeof ( RelationalJsonObject ) , parameters . Namespaces ) ;
492+ AddNamespace ( typeof ( JsonValueType ) , parameters . Namespaces ) ;
493+
494+ var columnVariable = parameters . ScopeVariables . TryGetValue ( column , out var cv )
495+ ? cv
496+ : $ "{ parameters . TargetName } .FindColumn({ code . Literal ( column . Name ) } )!";
497+ var elementVariable = CreateJsonElement ( column . JsonElement , columnVariable , parameters ) ;
498+
499+ mainBuilder . AppendLine ( $ "{ columnVariable } .JsonElement = { elementVariable } ;") ;
500+ }
501+ }
502+
503+ private string CreateJsonElement (
504+ IRelationalJsonElement element ,
505+ string columnVariable ,
506+ CSharpRuntimeAnnotationCodeGeneratorParameters parameters )
507+ {
508+ var parentLiteral = element . ParentElement != null && parameters . ScopeVariables . TryGetValue ( element . ParentElement , out var pv )
509+ ? pv
510+ : "null" ;
511+
512+ return element switch
513+ {
514+ IRelationalJsonObject jsonObject => CreateJsonObject ( jsonObject , columnVariable , parentLiteral , parameters ) ,
515+ IRelationalJsonArray jsonArray => CreateJsonArray ( jsonArray , columnVariable , parentLiteral , parameters ) ,
516+ IRelationalJsonScalar jsonProperty => CreateJsonProperty ( jsonProperty , columnVariable , parentLiteral , parameters ) ,
517+ _ => throw new UnreachableException ( )
518+ } ;
519+ }
520+
521+ private string CreateJsonObject (
522+ IRelationalJsonObject jsonObject ,
523+ string columnVariable ,
524+ string parentLiteral ,
525+ CSharpRuntimeAnnotationCodeGeneratorParameters parameters )
526+ {
527+ var code = Dependencies . CSharpHelper ;
528+ var mainBuilder = parameters . MainBuilder ;
529+ var variable = code . Identifier ( ( jsonObject . PropertyName ?? "element" ) + "JsonObject" , jsonObject , parameters . ScopeObjects , capitalize : false ) ;
530+
531+ mainBuilder . Append ( $ "var { variable } = new RelationalJsonObject(") ;
532+ AppendJsonConstructorArgs ( jsonObject , columnVariable , parentLiteral , mainBuilder , code ) ;
533+ mainBuilder . AppendLine ( ");" ) ;
534+
535+ foreach ( var child in jsonObject . Properties )
536+ {
537+ var childVariable = CreateJsonElement ( child , columnVariable , parameters with { ScopeVariables = new Dictionary < object , string > ( parameters . ScopeVariables ) { [ jsonObject ] = variable } } ) ;
538+ mainBuilder . AppendLine ( $ "{ variable } .AddProperty({ childVariable } );") ;
539+ }
540+
541+ return variable ;
542+ }
543+
544+ private string CreateJsonArray (
545+ IRelationalJsonArray jsonArray ,
546+ string columnVariable ,
547+ string parentLiteral ,
548+ CSharpRuntimeAnnotationCodeGeneratorParameters parameters )
549+ {
550+ var code = Dependencies . CSharpHelper ;
551+ var mainBuilder = parameters . MainBuilder ;
552+
553+ var variable = code . Identifier ( ( jsonArray . PropertyName ?? "array" ) + "JsonArray" , jsonArray , parameters . ScopeObjects , capitalize : false ) ;
554+
555+ mainBuilder . Append ( $ "var { variable } = new RelationalJsonArray(") ;
556+ AppendJsonConstructorArgs ( jsonArray , columnVariable , parentLiteral , mainBuilder , code ) ;
557+ mainBuilder . AppendLine ( ");" ) ;
558+
559+ var elementTypeVariable = CreateJsonElement ( jsonArray . ElementType , columnVariable ,
560+ parameters with { ScopeVariables = new Dictionary < object , string > ( parameters . ScopeVariables ) { [ jsonArray ] = variable } } ) ;
561+ mainBuilder . AppendLine ( $ "{ variable } .ElementType = { elementTypeVariable } ;") ;
562+
563+ return variable ;
564+ }
565+
566+ private string CreateJsonProperty (
567+ IRelationalJsonScalar jsonProperty ,
568+ string columnVariable ,
569+ string parentLiteral ,
570+ CSharpRuntimeAnnotationCodeGeneratorParameters parameters )
571+ {
572+ var code = Dependencies . CSharpHelper ;
573+ var mainBuilder = parameters . MainBuilder ;
574+ var variable = code . Identifier ( ( jsonProperty . PropertyName ?? "scalar" ) + "JsonScalar" , jsonProperty , parameters . ScopeObjects , capitalize : false ) ;
575+
576+ mainBuilder . Append ( $ "var { variable } = new RelationalJsonScalar(") ;
577+ AppendJsonConstructorArgs ( jsonProperty , columnVariable , parentLiteral , mainBuilder , code ) ;
578+ mainBuilder . Append ( $ ", JsonValueType.{ jsonProperty . ValueType } )") . AppendLine ( ";" ) ;
579+
580+ return variable ;
581+ }
582+
583+ private static void AppendJsonConstructorArgs (
584+ IRelationalJsonElement element ,
585+ string columnVariable ,
586+ string parentLiteral ,
587+ IndentedStringBuilder builder ,
588+ ICSharpHelper code )
589+ {
590+ if ( element . ParentElement is IRelationalJsonArray )
591+ {
592+ // (parent, isNullable) — array type
593+ builder . Append ( $ "{ parentLiteral } , { code . Literal ( element . IsNullable ) } ") ;
594+ }
595+ else if ( element . ParentElement is IRelationalJsonObject )
596+ {
597+ // (name, parent, isNullable) — object property
598+ builder . Append ( $ "{ code . Literal ( element . PropertyName ! ) } , { parentLiteral } , { code . Literal ( element . IsNullable ) } ") ;
599+ }
600+ else
601+ {
602+ // (column, isNullable) — root element
603+ builder . Append ( $ "{ columnVariable } , { code . Literal ( element . IsNullable ) } ") ;
604+ }
605+ }
606+
472607 private string GetOrCreate (
473608 ISqlQuery sqlQuery ,
474609 CSharpRuntimeAnnotationCodeGeneratorParameters parameters )
@@ -2078,6 +2213,7 @@ public override void Generate(IProperty property, CSharpRuntimeAnnotationCodeGen
20782213 annotations . Remove ( RelationalAnnotationNames . UpdateStoredProcedureParameterMappings ) ;
20792214 annotations . Remove ( RelationalAnnotationNames . UpdateStoredProcedureResultColumnMappings ) ;
20802215 annotations . Remove ( RelationalAnnotationNames . DefaultColumnMappings ) ;
2216+ annotations . Remove ( RelationalAnnotationNames . JsonElementMappings ) ;
20812217 }
20822218 else
20832219 {
@@ -2110,6 +2246,40 @@ public override void Generate(IProperty property, CSharpRuntimeAnnotationCodeGen
21102246 base . Generate ( property , parameters ) ;
21112247 }
21122248
2249+ /// <inheritdoc />
2250+ public override void Generate ( INavigation navigation , CSharpRuntimeAnnotationCodeGeneratorParameters parameters )
2251+ {
2252+ if ( parameters . IsRuntime )
2253+ {
2254+ var annotations = parameters . Annotations ;
2255+ annotations . Remove ( RelationalAnnotationNames . TableColumnMappings ) ;
2256+ annotations . Remove ( RelationalAnnotationNames . ViewColumnMappings ) ;
2257+ annotations . Remove ( RelationalAnnotationNames . SqlQueryColumnMappings ) ;
2258+ annotations . Remove ( RelationalAnnotationNames . FunctionColumnMappings ) ;
2259+ annotations . Remove ( RelationalAnnotationNames . DefaultColumnMappings ) ;
2260+ annotations . Remove ( RelationalAnnotationNames . JsonElementMappings ) ;
2261+ }
2262+
2263+ base . Generate ( navigation , parameters ) ;
2264+ }
2265+
2266+ /// <inheritdoc />
2267+ public override void Generate ( IComplexProperty complexProperty , CSharpRuntimeAnnotationCodeGeneratorParameters parameters )
2268+ {
2269+ if ( parameters . IsRuntime )
2270+ {
2271+ var annotations = parameters . Annotations ;
2272+ annotations . Remove ( RelationalAnnotationNames . TableColumnMappings ) ;
2273+ annotations . Remove ( RelationalAnnotationNames . ViewColumnMappings ) ;
2274+ annotations . Remove ( RelationalAnnotationNames . SqlQueryColumnMappings ) ;
2275+ annotations . Remove ( RelationalAnnotationNames . FunctionColumnMappings ) ;
2276+ annotations . Remove ( RelationalAnnotationNames . DefaultColumnMappings ) ;
2277+ annotations . Remove ( RelationalAnnotationNames . JsonElementMappings ) ;
2278+ }
2279+
2280+ base . Generate ( complexProperty , parameters ) ;
2281+ }
2282+
21132283 private void Create (
21142284 IRelationalPropertyOverrides overrides ,
21152285 string overridesVariable ,
0 commit comments