private bool OverrideCreatorIfNeeded(Type type, ModelObjectSerializationContext context, JsonContract result)
 {
     if (result is JsonObjectContract objectContract)
     {
         var provider = GetFactoryProvider(type, objectContract);
         if (provider != null)
         {
             objectContract.OverrideCreator = provider.Invoke(context);
             return(true);
         }
     }
     return(false);
 }
 /// <summary>
 /// Gets the contract for a given type.
 /// </summary>
 /// <param name="type">The type.</param>
 /// <param name="context">The context.</param>
 /// <returns>The contract for a given type.</returns>
 internal JsonContract GetContract(Type type, ModelObjectSerializationContext context)
 {
     if (!_cache.TryGetValue(type, out var result))
     {
         result = CreateContract(type);
         var updated = OverrideCreatorIfNeeded(type, context, result);
         if (!updated)
         {
             _cache[type] = result;
         }
     }
     return(result);
 }
예제 #3
0
 public JsonRepositorySerializer(ModelObjectContractCache modelObjectContractCache, ModelObjectSpecialValueProvider specialValueProvider,
                                 ModelObjectSerializationContext context = null)
 {
     _specialValueProvider = specialValueProvider ?? throw new ArgumentNullException(nameof(specialValueProvider));
     _serializer           = new JsonSerializer
     {
         NullValueHandling     = NullValueHandling.Ignore,
         ContractResolver      = CreateContractResolver(modelObjectContractCache, context),
         TypeNameHandling      = TypeNameHandling.Objects,
         SerializationBinder   = NetCoreSerializationBinder.Instance,
         ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
         Formatting            = Formatting.Indented,
         DefaultValueHandling  = DefaultValueHandling.IgnoreAndPopulate,
     };
     _serializer.Converters.Add(new VersionConverter());
 }
        private IModelObject LoadEntry(IObjectRepositoryContainer container, ObjectId commitId, TreeEntry entry, string path, Func <string, string> relativeFileDataResolver)
        {
            var context    = new ModelObjectSerializationContext(container, ResolveChildren);
            var serializer = _repositorySerializerFactory(context);
            var blob       = (Blob)entry.Target;

            return(serializer.Deserialize(blob.GetContentStream(), relativeFileDataResolver));

            ILazyChildren ResolveChildren(Type type, string propertyName)
            {
                var dataAccessor  = _dataAccessorProvider.Get(type);
                var childProperty = dataAccessor.ChildProperties.TryGetWithValue(p => p.Name, propertyName);

                if (childProperty == null)
                {
                    throw new GitObjectDbException($"Unable to find property details for '{propertyName}'.");
                }
                return(LoadEntryChildren(container, commitId, path, childProperty));
            }
        }
예제 #5
0
        private IEnumerable <Migrator> GetLogMigrators(IRepository repository, ICommitLog log, List <IMigration> deferred, Commit previousCommit, MigrationMode mode)
        {
            var context    = new ModelObjectSerializationContext(_container);
            var serializer = _repositorySerializerFactory(context);

            foreach (var commit in log)
            {
                if (previousCommit != null)
                {
                    var migrations = GetCommitMigrations(repository, previousCommit, commit, serializer).ToList();

                    deferred.AddRange(migrations.Where(m => m.IsIdempotent));

                    migrations.RemoveAll(m => m.IsIdempotent);
                    if (migrations.Any())
                    {
                        yield return(new Migrator(migrations.Where(m => !m.IsIdempotent).ToImmutableList(), mode, commit.Id));
                    }
                }
                previousCommit = commit;
            }
        }
 public ModelObjectContractResolver(ModelObjectSerializationContext context,
                                    ModelObjectContractCache modelObjectContractCache)
 {
     _context = context ?? throw new ArgumentNullException(nameof(context));
     _modelObjectContractCache = modelObjectContractCache ?? throw new ArgumentNullException(nameof(modelObjectContractCache));
 }
 private static JsonRepositorySerializer CreateJsonRepositorySerializer(IServiceProvider serviceProvider, ModelObjectSerializationContext context = null) =>
 new JsonRepositorySerializer(
     serviceProvider.GetRequiredService <ModelObjectContractCache>(),
     serviceProvider.GetRequiredService <ModelObjectSpecialValueProvider>(),
     context);
예제 #8
0
 private static CamelCasePropertyNamesContractResolver CreateContractResolver(ModelObjectContractCache modelObjectContractCache, ModelObjectSerializationContext context) =>
 context != null ? new ModelObjectContractResolver(context, modelObjectContractCache) : new CamelCasePropertyNamesContractResolver();