Skip to content

Commit d16d2ec

Browse files
finishing and testing readAndParse, implementing ability for custom
formating at Serializer lvl + ability to disable formatting completely (important), modif. some converters accordingly (Json specif logic separation into JsonVarConverter...). Optimizations and small API changes
1 parent a6d5112 commit d16d2ec

File tree

17 files changed

+464
-256
lines changed

17 files changed

+464
-256
lines changed

SerialX-core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<groupId>org.ugp.serialx</groupId>
1212
<artifactId>serialx-core</artifactId>
13-
<version>1.3.8</version>
13+
<version>1.3.9-SNAPSHOT</version>
1414

1515
<name>SerialX core</name>
1616
<description>Core of SerialX. Contains core features and utilities shared across the library.</description>

SerialX-core/src/main/java/org/ugp/serialx/Serializer.java

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public abstract class Serializer extends Scope
5959
protected ParserRegistry parsers;
6060
protected ProtocolRegistry protocols;
6161

62+
protected byte format = 0;
63+
6264
/**
6365
* @param values | Initial independent values to be added in to this scope!
6466
*
@@ -257,16 +259,17 @@ public String toString(boolean serialize) throws IOException
257259

258260
/**
259261
* @param f | File to write in. This must be a text file.
262+
* @param args | Additional arguments to use, exact usage and behavior of them is based on specific implementation of this function (they should not be serialized)!
260263
*
261264
* @return String with variables and objects serialized in specific format.
262265
*
263266
* @throws IOException if file can't be opened or serialization fails!
264267
*
265268
* @since 1.1.5
266269
*/
267-
public void SerializeTo(File f) throws IOException
270+
public void SerializeTo(File f, Object... args) throws IOException
268271
{
269-
SerializeTo(false, f);
272+
SerializeTo(false, f, args);
270273
}
271274

272275
/**
@@ -472,9 +475,23 @@ public <T extends Serializer> T emptyClone(T newEmptyInstance, GenericScope<?, ?
472475
{
473476
newEmptyInstance.setParsers(getParsers());
474477
newEmptyInstance.setProtocols(getProtocols());
478+
newEmptyInstance.setFormat(getFormat());
475479
newEmptyInstance.parent = parent;
476480
return newEmptyInstance;
477-
}
481+
}
482+
483+
@Override
484+
public Scope clone()
485+
{
486+
Scope scope = super.clone();
487+
if (scope instanceof Serializer)
488+
{
489+
((Serializer) scope).setParsers(getParsers());
490+
((Serializer) scope).setProtocols(getProtocols());
491+
((Serializer) scope).setFormat(getFormat());
492+
}
493+
return scope;
494+
}
478495

479496
/**
480497
* @return {@link Registry} with parsers that this {@link Serializer} uses!
@@ -607,18 +624,27 @@ public <A extends Appendable> A SerializeAsSubscope(A source, Object... args) th
607624
* @since 1.3.5
608625
*/
609626
@SuppressWarnings("unchecked")
610-
public <A extends Appendable> A SerializeAsSubscope(A source, char[] wrappingBrackets, Object... args) throws IOException
627+
public <A extends Appendable> A SerializeAsSubscope(A source, final char[] wrappingBrackets, Object... args) throws IOException
611628
{
612-
int tabs = 0;
613-
if (args.length > 1 && args[1] instanceof Integer)
629+
source.append(wrappingBrackets[0]);
630+
if (isEmpty())
631+
return (A) source.append(wrappingBrackets[1]);
632+
633+
if (format == 0) // No format...
614634
{
615-
tabs = (int) args[1];
616-
args[1] = tabs + 1;
635+
if (args.length > 1 && args[1] instanceof Integer)
636+
args[1] = ((int) args[1]) + 1;
637+
return (A) SerializeTo(source, args).append(wrappingBrackets[1]);
617638
}
618-
source.append(wrappingBrackets[0]);
619-
if (!isEmpty())
620-
source = (A) SerializeTo(source.append('\n'), args).append('\n').append(multilpy('\t', tabs));
621-
return (A) source.append(wrappingBrackets[1]);
639+
640+
int tabs;
641+
if (args.length > 1 && args[1] instanceof Integer)
642+
args[1] = (tabs = (int) args[1]) + 1;
643+
else
644+
tabs = 0;
645+
646+
return (A) SerializeTo(source.append('\n'), args).append('\n').append(multilpy('\t', tabs))
647+
.append(wrappingBrackets[1]);
622648
}
623649

624650
/**
@@ -648,6 +674,28 @@ public <T> T getParsed(String variableWithStringValue, Object... args)
648674
{
649675
return (T) getParsers().parse(getString(variableWithStringValue), args);
650676
}
677+
678+
/**
679+
* @return Non 0 value if proper indentation and newline characters should be used when serializing. Default is 0, no formating...
680+
* The exact behavior of other values will depend on the specific implementation of {@link Serializer#SerializeTo(Appendable, Object...)}
681+
*
682+
* @since 1.3.9
683+
*/
684+
public byte getFormat()
685+
{
686+
return format;
687+
}
688+
689+
/**
690+
* @param format | A non 0 value if proper indentation and newline characters should be used when serializing.
691+
* The exact behavior of other values will depend on the specific implementation of {@link Serializer#SerializeTo(Appendable, Object...)}
692+
*
693+
* @since 1.3.9
694+
*/
695+
public void setFormat(byte format)
696+
{
697+
this.format = format;
698+
}
651699

652700
/**
653701
* @param obj | Object to map the serializer variables into!
@@ -773,7 +821,7 @@ public static <T> T into(Object obj, Serializer fromSerializer, String... fieldN
773821

774822
return (T) Scope.into(obj, fromSerializer, fieldNamesToUse);
775823
}
776-
824+
777825
/**
778826
* @param newInstance | New instance of specific {@link Serializer}
779827
* @param fromObj | Object to create serializer from!

SerialX-core/src/main/java/org/ugp/serialx/Utils.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.net.URLEncoder;
1818
import java.util.ArrayList;
1919
import java.util.Arrays;
20+
import java.util.Collection;
2021
import java.util.List;
2122
import java.util.Map;
2223

@@ -35,6 +36,13 @@
3536
public final class Utils {
3637
private Utils() {}
3738

39+
/**
40+
* {@link System#lineSeparator()}
41+
*
42+
* @since 1.3.9
43+
*/
44+
public static final char[] ENDL = System.lineSeparator().toCharArray();
45+
3846
/**
3947
* @param f | Source file.
4048
*
@@ -235,7 +243,7 @@ public static <T> T Clone(T obj)
235243
* @since 1.3.2
236244
*/
237245
@SuppressWarnings("unchecked")
238-
public static <T> T Clone(T obj, Registry<DataParser> parsersToUse, Object[] converterArgs, Object... parserArgs)
246+
public static <T> T Clone(T obj, Collection<DataParser> parsersToUse, Object[] converterArgs, Object... parserArgs)
239247
{
240248
if (obj == null)
241249
return null;
@@ -749,18 +757,16 @@ public static String fastReplace(String str, String target, CharSequence replace
749757
* @param ch | Char to compare!
750758
* @param chars | Chars to match!
751759
*
752-
* @return True if inserted char is any of inserted chars!
760+
* @return True if inserted char (ch) is any of inserted chars!
753761
*
754762
* @since 1.3.0
755763
*/
756764
public static boolean isOneOf(int ch, char... chars)
757765
{
758-
if (chars.length > 0)
759-
{
760-
for (int i = 0, len = chars.length; i < len; i++)
761-
if (chars[i] == ch)
762-
return true;
763-
}
766+
final int charsLen = chars.length;
767+
for (int i = 0; i < charsLen; i++)
768+
if (chars[i] == ch)
769+
return true;
764770
return false;
765771
}
766772

@@ -821,7 +827,7 @@ public static String showPosInString(CharSequence str, int pos)
821827

822828
try
823829
{
824-
if (contains(str, '\n', '\r'))
830+
if (contains(str, ENDL))
825831
return str.subSequence(0, pos) + "»" + str.subSequence(pos, str.length());
826832
return multilpy(' ', pos).append('^').insert(0, '\n').insert(0, str).toString();
827833
}
@@ -881,14 +887,14 @@ public static Object[] fromAmbiguousArray(Object array)
881887
{
882888
if (array instanceof Object[])
883889
return (Object[]) array;
884-
885-
int len = Array.getLength(array);
890+
891+
int len = Array.getLength(array); // Arr of primitives cos Java...
886892
Object[] arr = new Object[len];
887893
for (int i = 0; i < len; i++)
888894
arr[i] = Array.get(array, i);
889895
return arr;
890896
}
891-
897+
892898
/* Others... */
893899

894900
/**

SerialX-core/src/main/java/org/ugp/serialx/converters/BooleanConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public BooleanConverter()
5656
*
5757
* @since 1.3.5
5858
*/
59-
public BooleanConverter(boolean shorten)
59+
public BooleanConverter(boolean shorten)
6060
{
6161
setShorten(shorten);
6262
}

SerialX-core/src/main/java/org/ugp/serialx/converters/ProtocolConverter.java

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.ugp.serialx.converters;
22

3-
import static org.ugp.serialx.Utils.Instantiate;
3+
import static org.ugp.serialx.Utils.*;
44
import static org.ugp.serialx.Utils.indexOfNotInObj;
55
import static org.ugp.serialx.Utils.splitValues;
66

@@ -193,24 +193,25 @@ public CharSequence toString(ParserRegistry myHomeRegistry, Object obj, Serializ
193193

194194
if (preferedProtocol != null || (preferedProtocol = (SerializationProtocol<Object>) getProtocolFor(obj, SerializationProtocol.MODE_SERIALIZE | getProtocolType(), args)) != null)
195195
{
196-
Class<?> oldObjectClass = null;
196+
if (args.length < 6)
197+
args = Arrays.copyOf(args, 6);
198+
Class<?> oldObjectClass = (Class<?>) args[4];
199+
args[4] = obj.getClass();
200+
201+
int tabs = args[1] instanceof Integer ? (int) args[1] : 0;
202+
int index = args[2] instanceof Integer ? (int) args[2] : 0;
203+
197204
try
198205
{
199-
int tabs = args.length > 1 && args[1] instanceof Integer ? (int) args[1] : 0;
200-
int index = args.length > 2 && args[2] instanceof Integer ? (int) args[2] : 0;
201-
202-
if (args.length < 5)
203-
args = Arrays.copyOf(args, 5);
204-
oldObjectClass = (Class<?>) args[4];
205-
args[4] = obj.getClass();;
206-
207206
Object[] objArgs = preferedProtocol.serialize(obj);
208-
StringBuilder sb = new StringBuilder(ImportsProvider.getAliasFor(args.length > 0 ? args[0] : null, obj.getClass()) + (objArgs.length <= 0 ? "" : " "));
209-
207+
final int len = objArgs.length;
208+
209+
StringBuilder sb = new StringBuilder(ImportsProvider.getAliasFor(args[0], obj.getClass()));
210210
args = args.clone();
211-
for (int i = 0, sizeEndl = 10000; i < objArgs.length; i++)
211+
if (args[5] instanceof Byte && (byte) args[5] != 0) // Format
212212
{
213-
if (i > 0)
213+
for (int i = 0, sizeEndl = 10000; i < len; i++)
214+
{
214215
if (sb.length() > sizeEndl)
215216
{
216217
sb.append('\n');
@@ -220,14 +221,26 @@ public CharSequence toString(ParserRegistry myHomeRegistry, Object obj, Serializ
220221
}
221222
else
222223
sb.append(' ');
223-
224-
if (args.length > 2)
225-
args[2] = index + 1;
226-
sb.append(myHomeRegistry.toString(objArgs[i], args));
224+
225+
if (args.length > 2)
226+
args[2] = index + 1; // DO NOT TOUCH
227+
sb.append(myHomeRegistry.toString(objArgs[i], args));
228+
}
229+
}
230+
else
231+
{
232+
for (int i = 0; i < len; i++)
233+
{
234+
sb.append(' ');
235+
236+
if (args.length > 2)
237+
args[2] = index + 1; // DO NOT TOUCH
238+
sb.append(myHomeRegistry.toString(objArgs[i], args));
239+
}
227240
}
228241

229242
args[4] = oldObjectClass;
230-
return index > 0 && objArgs.length > 0 ? sb.insert(0, '{').append('}') : sb;
243+
return index > 0 && len != 0 ? sb.insert(0, '{').append('}') : sb;
231244
}
232245
catch (Exception e)
233246
{
@@ -242,7 +255,7 @@ public CharSequence toString(ParserRegistry myHomeRegistry, Object obj, Serializ
242255
@Override
243256
public CharSequence getDescription(ParserRegistry myHomeRegistry, Object obj, Object... argsUsedConvert)
244257
{
245-
if (obj instanceof CharSequence && indexOfNotInObj((CharSequence) obj, '\n', '\r') != -1)
258+
if (obj instanceof CharSequence && indexOfNotInObj((CharSequence) obj, ENDL) != -1)
246259
return "Multiline char sequence!";
247260
return new StringBuilder("Object of ").append(obj.getClass().getName()).append(": \"").append(obj.toString()).append("\" serialized using ").append(getProtocolFor(obj, SerializationProtocol.MODE_SERIALIZE_DESERIALIZE | getProtocolType(), argsUsedConvert)).append("!");
248261
}

SerialX-core/src/main/java/org/ugp/serialx/converters/imports/ImportsProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public interface ImportsProvider
3838
*
3939
* @since 1.3.5
4040
*/
41-
public Imports getImports();
41+
Imports getImports();
4242

4343
/**
4444
* @param obj | Object to get imports from (its supposed to be instance of {@link ImportsProvider} or collection of {@link Import})!

SerialX-devtools/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<dependency>
2020
<groupId>org.ugp.serialx</groupId>
2121
<artifactId>serialx-core</artifactId>
22-
<version>${revision}</version>
22+
<version>1.3.8</version>
2323
</dependency>
2424
</dependencies>
2525
</project>

SerialX-json/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<groupId>org.ugp.serialx</groupId>
1212
<artifactId>serialx-json</artifactId>
13-
<version>1.3.8</version>
13+
<version>1.3.9-SNAPSHOT</version>
1414

1515
<name>SerialX json</name>
1616
<description>SerialX Json support</description>
@@ -19,7 +19,7 @@
1919
<dependency>
2020
<groupId>org.ugp.serialx</groupId>
2121
<artifactId>serialx-juss</artifactId>
22-
<version>${revision}</version>
22+
<version>1.3.9-SNAPSHOT</version>
2323
</dependency>
2424
</dependencies>
2525
</project>

0 commit comments

Comments
 (0)