public static CodeBlock ResourceDocument(
            CompilerContext compilerContext,
            CodeGenerationContext codeGenContext)
        {
            var contextUri = StringMethods.Concat(codeGenContext.BaseUri, New.Const(CompilerContext.ContextUri));

            var rootNode = WriteNode(
                compilerContext,
                codeGenContext,
                new[]
            {
                WriteContext(codeGenContext.JsonWriter, contextUri)
            });

            return(new CodeBlock(
                       codeGenContext.JsonWriter.WriteBeginObject(),
                       rootNode,
                       codeGenContext.JsonWriter.WriteEndObject()
                       ));
        }
Exemple #2
0
        public static CodeBlock ResourceDocument(
            Variable <JsonWriter> jsonWriter,
            ResourceModel model,
            Expression resource,
            Variable <SerializationContext> options,
            IMetaModelRepository models)
        {
            var uriGenerator  = options.get_UriGenerator();
            var typeGenerator = options.get_TypeGenerator();
            var baseUri       = options.get_BaseUri().ObjectToString();
            var contextUri    = StringMethods.Concat(baseUri, New.Const(".hydra/context.jsonld"));

            var jsonFormatterResolver = New.Var <HydraJsonFormatterResolver>("resolver");
            var assignResolver        = jsonFormatterResolver.Assign(New.Instance <HydraJsonFormatterResolver>());

            var rootNode = WriteNode(
                jsonWriter,
                baseUri,
                model,
                resource,
                uriGenerator,
                typeGenerator,
                models,
                jsonFormatterResolver,
                new[]
            {
                WriteContext(jsonWriter, contextUri)
            });

            return(new CodeBlock(
                       jsonFormatterResolver,
                       assignResolver,
                       jsonWriter.WriteBeginObject(),
                       rootNode,
                       jsonWriter.WriteEndObject()
                       ));
        }
Exemple #3
0
        static IEnumerable <NodeProperty> GetNodeProperties(
            Variable <JsonWriter> jsonWriter,
            TypedExpression <string> baseUri,
            ResourceModel model,
            Expression resource,
            MemberAccess <Func <object, string> > uriGenerator,
            MemberAccess <Func <object, string> > typeGenerator,
            IMetaModelRepository models,
            Stack <ResourceModel> recursionDefender,
            Variable <HydraJsonFormatterResolver> jsonFormatterResolver,
            Type resourceType,
            TypedExpression <string> resourceUri,
            string resourceRegistrationHydraType)
        {
            var publicProperties = resourceType
                                   .GetProperties(BindingFlags.Public | BindingFlags.Instance)
                                   .Where(HydraTextExtensions.IsNotIgnored)
                                   .ToList();

            var propNames     = publicProperties.Select(HydraTextExtensions.GetJsonPropertyName);
            var overridesId   = propNames.Any(name => name == "@id");
            var overridesType = propNames.Any(name => name == "@type");

            if (overridesId == false && model.Uris.Any())
            {
                yield return(WriteId(jsonWriter, resourceUri));
            }

            if (overridesType == false)
            {
                var typePropertyFactory = model.Hydra().TypeFunc;
                if (typePropertyFactory == null)
                {
                    yield return(WriteType(jsonWriter, resourceRegistrationHydraType));
                }
                else
                {
                    yield return(WriteType(jsonWriter, StringMethods.Concat(baseUri, typeGenerator.Invoke(resource))));
                }
            }


            foreach (var pi in publicProperties)
            {
                if (pi.GetIndexParameters().Any())
                {
                    continue;
                }

                if (pi.PropertyType == typeof(string) ||
                    (pi.PropertyType.IsValueType && Nullable.GetUnderlyingType(pi.PropertyType) == null))
                {
                    var nodePropertyValue = WriteNodePropertyValue(
                        jsonWriter,
                        pi,
                        jsonFormatterResolver,
                        resource);

                    yield return(nodePropertyValue);

                    continue;
                }

                yield return(WriteNodeProperty(
                                 jsonWriter, baseUri, resource, uriGenerator, typeGenerator, models, recursionDefender, pi,
                                 jsonFormatterResolver));
            }

            foreach (var link in model.Links)
            {
                yield return(WriteNodeLink(jsonWriter, link.Relationship, link.Uri, resourceUri, link));
            }
        }