<commit_before>package com.tinkerpop.gremlin.structure.io.graphson;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.StdKeySerializer;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.tinkerpop.gremlin.structure.Edge;
import com.tinkerpop.gremlin.structure.Element;
import com.tinkerpop.gremlin.structure.VertexProperty;
import com.tinkerpop.gremlin.structure.Property;
import com.tinkerpop.gremlin.structure.Vertex;
import com.tinkerpop.gremlin.util.StreamFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author <NAME> (http://stephen.genoprime.com)
 */
public class GraphSONModule extends SimpleModule {

    public GraphSONModule(final boolean normalize) {
        super("graphson");
        addSerializer(Edge.class, new EdgeJacksonSerializer());
        addSerializer(Vertex.class, new VertexJacksonSerializer());
        addSerializer(GraphSONVertex.class, new GraphSONVertex.VertexJacksonSerializer());
        addSerializer(GraphSONGraph.class, new GraphSONGraph.GraphJacksonSerializer(normalize));
        addSerializer(VertexProperty.class, new VertexPropertyJacksonSerializer());
    }

    static class VertexPropertyJacksonSerializer extends StdSerializer<VertexProperty> {
        public VertexPropertyJacksonSerializer() {
            super(VertexProperty.class);
        }

        @Override
        public void serialize(final VertexProperty property, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider)
                throws IOException {
            ser(property, jsonGenerator);
        }

        @Override
        public void serializeWithType(final VertexProperty property, final JsonGenerator jsonGenerator,
                                      final SerializerProvider serializerProvider, final TypeSerializer typeSerializer) throws IOException {
            ser(property, jsonGenerator);
        }

        private void ser(final VertexProperty property, final JsonGenerator jsonGenerator) throws IOException {
            final Map<String, Object> m = new HashMap<>();
            m.put(GraphSONTokens.ID, property.id());
            m.put(GraphSONTokens.LABEL, property.label());
            m.put(GraphSONTokens.VALUE, property.value());
            m.put(GraphSONTokens.PROPERTIES, StreamFactory.<Property<Object>>stream(property.iterators().properties()).collect(Collectors.toMap(Property::key, Property::value)));
            m.put(GraphSONTokens.HIDDENS, StreamFactory.<Property<Object>>stream(property.iterators().hiddens()).collect(Collectors.toMap(Property::key, Property::value)));

            jsonGenerator.writeObject(m);
        }
    }

    static class EdgeJacksonSerializer extends StdSerializer<Edge> {
        public EdgeJacksonSerializer() {<commit_msg>Catch graphs that dont' support properties on properties in GraphSON serialization #161<commit_after>package com.tinkerpop.gremlin.structure.io.graphson;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.StdKeySerializer;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.tinkerpop.gremlin.structure.Edge;
import com.tinkerpop.gremlin.structure.Element;
import com.tinkerpop.gremlin.structure.VertexProperty;
import com.tinkerpop.gremlin.structure.Property;
import com.tinkerpop.gremlin.structure.Vertex;
import com.tinkerpop.gremlin.util.StreamFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author <NAME> (http://stephen.genoprime.com)
 */
public class GraphSONModule extends SimpleModule {

    public GraphSONModule(final boolean normalize) {
        super("graphson");
        addSerializer(Edge.class, new EdgeJacksonSerializer());
        addSerializer(Vertex.class, new VertexJacksonSerializer());
        addSerializer(GraphSONVertex.class, new GraphSONVertex.VertexJacksonSerializer());
        addSerializer(GraphSONGraph.class, new GraphSONGraph.GraphJacksonSerializer(normalize));
        addSerializer(VertexProperty.class, new VertexPropertyJacksonSerializer());
    }

    static class VertexPropertyJacksonSerializer extends StdSerializer<VertexProperty> {
        public VertexPropertyJacksonSerializer() {
            super(VertexProperty.class);
        }

        @Override
        public void serialize(final VertexProperty property, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider)
                throws IOException {
            ser(property, jsonGenerator);
        }

        @Override
        public void serializeWithType(final VertexProperty property, final JsonGenerator jsonGenerator,
                                      final SerializerProvider serializerProvider, final TypeSerializer typeSerializer) throws IOException {
            ser(property, jsonGenerator);
        }

        private void ser(final VertexProperty property, final JsonGenerator jsonGenerator) throws IOException {
            final Map<String, Object> m = new HashMap<>();
            m.put(GraphSONTokens.ID, property.id());
            m.put(GraphSONTokens.LABEL, property.label());
            m.put(GraphSONTokens.VALUE, property.value());

            Map<String,Object> properties;
            try {
                properties = StreamFactory.<Property<Object>>stream(property.iterators().properties()).collect(Collectors.toMap(Property::key, Property::value));
            } catch (UnsupportedOperationException uoe) {
                // todo: is there a way to get the feature down here so we can just test it directly?
                properties = new HashMap<>();
            }

            Map<String,Object> hiddens;
            try {
                hiddens = StreamFactory.<Property<Object>>stream(property.iterators().hiddens()).collect(Collectors.toMap(Property::key, Property::value));
            } catch (UnsupportedOperationException uoe) {
                // todo: is there a way to get the feature down here so we can just test it directly?
                hiddens = new HashMap<>();
            }

            m.put(GraphSONTokens.PROPERTIES, properties);
            m.put(GraphSONTokens.HIDDENS, hiddens);

            jsonGenerator.writeObject(m);
        }
    }

    static class EdgeJacksonSerializer extends StdSerializer<Edge> {
        public EdgeJacksonSerializer() {
{'fraction_non_alphanumeric': 0.0936631055173423, 'fraction_numerical': 0.00043904580711254205, 'mean_word_length': 3.565130260521042, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3114598', 'n_tokens_mistral': 1800, 'n_tokens_neox': 1680, 'n_words': 351}